G
Guest
I have a hashtable keyed by some name, holding an instance of an object. Most
of the time I use the hashtable in the traditional sense, given a name, I
lookup the object, then I run a method on that object. Occaisionally however
I need to run a method on every object in the hashtable. I've done both
For..Each and iEnumeratior loops. The messy part is that under certain
conditions, while iterating, a condition exists where I need to remove an
object from the hashtable. With ArrayLists I handle this with a reverse
iteration (from last to first) and do a .RemoveAt. Hashtables do not have
..RemoveAt. For..Each doesnt work because .Remove needs the key and I only
have the object, iEnumerator doesn't work because I get an error after the
first .Remove saying my enumerator is now invalid, so I can't continue that
iteration.
I've thought about several solutions mostly inlvolving separate threads, but
wanted to know if there was a cleaner way to handle this. Anyone come across
this? It seems like a fairly standard problem, there must be some "pattern"
solution.
-Ken
of the time I use the hashtable in the traditional sense, given a name, I
lookup the object, then I run a method on that object. Occaisionally however
I need to run a method on every object in the hashtable. I've done both
For..Each and iEnumeratior loops. The messy part is that under certain
conditions, while iterating, a condition exists where I need to remove an
object from the hashtable. With ArrayLists I handle this with a reverse
iteration (from last to first) and do a .RemoveAt. Hashtables do not have
..RemoveAt. For..Each doesnt work because .Remove needs the key and I only
have the object, iEnumerator doesn't work because I get an error after the
first .Remove saying my enumerator is now invalid, so I can't continue that
iteration.
I've thought about several solutions mostly inlvolving separate threads, but
wanted to know if there was a cleaner way to handle this. Anyone come across
this? It seems like a fairly standard problem, there must be some "pattern"
solution.
-Ken