G
Guest
Hi all,
I wanted to go through each entry(?) of ArrayList and remove some particular
entry. So I tried following but it throws exception at runtime:
foreach (myEntry entry in myArrayList)
{
// do something...
if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}
/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
*/
So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a better
way?
Regards
Bob
I wanted to go through each entry(?) of ArrayList and remove some particular
entry. So I tried following but it throws exception at runtime:
foreach (myEntry entry in myArrayList)
{
// do something...
if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}
/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
*/
So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a better
way?
Regards
Bob