473,587 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ 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(objec tA)

}

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 1162
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(objec tA)

}

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>(li stA)) { // or listA.ToArray()
...
listA.Add(objec tA);
}

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(objec tA);
}

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

List<classAlist B = new List<classA>();
foreach (classA objectA in listA) {
...
listB.Add(objec tA);
}
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*******@yaho o.comwrote in message
news:OD******** *****@TK2MSFTNG P06.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(objec tA)

}

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
4219
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 assume the data types and thus can't sort them. I thought about implementing IComparable, but I don't see how that would work since it doesn't...
2
3167
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 problems that I have encountered to date and the solutions (if any) that I found....
9
1708
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 words: while we are inside this codeblock whoever might think of modified that particular collection has to wait until we have left that codeblock....
3
994
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 them to have a modification count variable that increments every time the particular collection is modified. Unfortunately I can't find anything...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.