Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove within foreach 2

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
SE
How can I remove something in the best way without getting "Collection was modified; enumeration operation may not execute."

foreach (SerieClass s in PointSeries)
{
PointSeries.Remove(s);
}

Thanks!

--- neteject.com - Internet Solutions ---
 
You can lock the PointSeries object while traversing it or use for
for (int i=0;i<PointSeries.Count;i++)
{
SerieClass sc = (SerieClass)(PointSeries.List);
}
obislavu
 
How about:

foreach (SerieClass s in PointSeries)
{
s.Remove();
}

Lee
 
How should the s.Remove() function look? I want to remove it from PointSeries ArrayList.

--- neteject.com - Internet Solutions ---
 
I thought you had the method written already. For this I'd get the index of s and then move all the objects in the array up one. That means you'll have to keep track of the index of each object, which would be just another property, when you assign them to the array. If you're deleting the whole array, though, you should do that rather than going through the whole list and deleting objects one by one.

Lee
 
How about this? It is the best way of removing something from an ArrayList while traversing it.

int iIndex;
while( iIndex < PointSeries.Count)
{
if(...) //if you decide to remove item at position iIndex
PointSeries.RemoveAt( iIndex);
else
iIndex++;
}

 
Looks nice B00gyeMan, I suppose you can't do anything similar to a hashlist, you never know which is the next value in a foreach?

--- neteject.com - Internet Solutions ---
 
You can't use a foreach when doing inserts or removals -- you get the "list was modified" errors. You'll have to use a standard for..next loop, or better yet, a while loop.

Also - sometimes collections have Clear() methods -- did you look at it?

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
I did look at it. I use a while loop instead. I just love the beauty of foreach :)

Thanks all!

--- neteject.com - Internet Solutions ---
 
IMO easiest way is just to loop through collection backwards. That way you can use the for construct and remove as many as you need to something like this
Code:
for(int i=PointSeries.Count-1; i>=0; i--){
 if(needsRemoving) PointSeries.RemoveAt(i);
}

Rob

[i]i'm a boy, called Bert, and I may not be crazy, but if i'm not the rest of you are...[/i]
 
That depends on the type of collection. Ones in which order is important (like ArrayList), can be looped thru backwards. Ones in which order is not important (like HashTable), probably wouldn't benefit from this technique.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top