473,473 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

collection modification problems

Hello,

I am trying to iterate through list:

foreach (classA objectA in listA)
{
//if there is no objectA with specific name in list A then:

listA.add(objectA)

}

But it seems like this is an exception to modify the collection that I
am iterationg through. How to solve this?

Thank you!
*** Sent via Developersdex http://www.developersdex.com ***
Oct 26 '08 #1
2 1158
csharpula csharp wrote:
I am trying to iterate through list:

foreach (classA objectA in listA)
{
//if there is no objectA with specific name in list A then:

listA.add(objectA)

}

But it seems like this is an exception to modify the collection that I
am iterationg through. How to solve this?
One option is to, well, not modify the collection you're iterating through,
but use a copy instead:

foreach (classA objectA in new List<classA>(listA)) { // or listA.ToArray()
...
listA.Add(objectA);
}

Another option is to iterate in a way that will not fail when new elements
are added:

int count = listA.Count;
for (int i = 0; i != count; ++i) {
classA objectA = listA[i];
...
listA.Add(objectA);
}

Finally, you could produce the results as a new list and combine them
afterwards:

List<classAlistB = new List<classA>();
foreach (classA objectA in listA) {
...
listB.Add(objectA);
}
listA.AddRange(listB);

If all you're actually doing is appending elements, option #2 will generally
be the most efficient since no additional memory is allocated beyond what
you need for the extra elements.

--
J.
Oct 26 '08 #2
"csharpula csharp" <cs*******@yahoo.comwrote in message
news:OD*************@TK2MSFTNGP06.phx.gbl...
Hello,

I am trying to iterate through list:

foreach (classA objectA in listA)
{
//if there is no objectA with specific name in list A then:

listA.add(objectA)

}

But it seems like this is an exception to modify the collection that I
am iterationg through. How to solve this?

Thank you!
*** Sent via Developersdex http://www.developersdex.com ***

I hope I understand you correctly.
You want to add an item to a collection if it is not already there?
Depending on the size of your list and the frequency of the search, you
could be adding unnecessary overhead to your application.
Have you considered using a collection type that does not allow duplicates?

http://msdn.microsoft.com/en-us/libr...s.generic.aspx

Oct 26 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
3
by: usenet.tolomea | last post by:
I'm working on a remote object system, something kinda like Pyro. For the purposes of caching I need to be able to tell if a given dict / list / set has been modified. Ideally what I'd like is for...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.