A
Alvin Bruney
Error:
Collection was modified; enumeration operation may not execute.
I've dodged this issue for a while now with workarounds but now i want to
stand up and fight.
I don't want to run away from it anymore.
How do I get around tampering with a collection within a loop and keep
iterating. Here is my code:
//Threadwork.Getlist returns a hash table
foreach(DictionaryEntry key in ThreadWork.GetList)
{
lock(ThreadWork.GetList)
ThreadWork.GetList.Remove(key.Key.ToString());
}
I know why it is happening, but I don't know how to fix it. I've had the
same issue with operations like this
foreach(listitem li in somelistbox.items)
{
trying to remove 2 li items in succession will throw the same exception
because the collection is modified.
}
I know how to work around it by using an arraylist as a temporary container
but I don't want a work around anymore. I want to fix it. I want to remove
the item and keep iterating without using a secondary container to store the
values. I think that workaround is ineligant, cheap and doesn't promote
robust coding. Yuck!
I'd like some help please.
Collection was modified; enumeration operation may not execute.
I've dodged this issue for a while now with workarounds but now i want to
stand up and fight.
I don't want to run away from it anymore.
How do I get around tampering with a collection within a loop and keep
iterating. Here is my code:
//Threadwork.Getlist returns a hash table
foreach(DictionaryEntry key in ThreadWork.GetList)
{
lock(ThreadWork.GetList)
ThreadWork.GetList.Remove(key.Key.ToString());
}
I know why it is happening, but I don't know how to fix it. I've had the
same issue with operations like this
foreach(listitem li in somelistbox.items)
{
trying to remove 2 li items in succession will throw the same exception
because the collection is modified.
}
I know how to work around it by using an arraylist as a temporary container
but I don't want a work around anymore. I want to fix it. I want to remove
the item and keep iterating without using a secondary container to store the
values. I think that workaround is ineligant, cheap and doesn't promote
robust coding. Yuck!
I'd like some help please.