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.Rem ove(entry); // exception below
} // remove entry
}
/* exception:
System.InvalidO perationExcepti on: Collection was modified; enumeration
operation may not execute.
at System.Collecti ons.ArrayList.A rrayListEnumera torSimple.MoveN ext()
*/
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.Rem ove(entry); // exception below
} // remove entry
}
/* exception:
System.InvalidO perationExcepti on: Collection was modified; enumeration
operation may not execute.
at System.Collecti ons.ArrayList.A rrayListEnumera torSimple.MoveN ext()
*/
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
Comment