PC Review


Reply
Thread Tools Rate Thread

collection modification problems

 
 
csharpula csharp
Guest
Posts: n/a
 
      26th Oct 2008
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 ***
 
Reply With Quote
 
 
 
 
Jeroen Mostert
Guest
Posts: n/a
 
      26th Oct 2008
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<classA> listB = 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.
 
Reply With Quote
 
PvdG42
Guest
Posts: n/a
 
      26th Oct 2008
"csharpula csharp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
Data entry, deletion and modification problems. Kong Microsoft Access Form Coding 3 9th Aug 2006 09:15 AM
registry modification causes some unexpected problems Chris-n-Jordan Windows XP Help 0 1st Mar 2006 03:54 PM
Data Collection problems John Ferrell Microsoft Frontpage 6 21st Jun 2004 01:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:50 PM.