Ok there is probably a very straight forward answer to this but I fail to understand how the enumeration of a hashtable works in C#. I have a hashtable (myHash) that I am trying to enumerate and loop through. The key is a string and the value is an int. All I am trying to do it decrease every value by 1. Everytime I run the code below it throws back an error (Collection was modified; enumeration operation may not execute).
How do I get around this?
int tmpInt;
Hashtable myHash = new Hashtable();
populate(myHash);
lock(myHash.SyncRoot)
{
IDictionaryEnumerator hashEn = myHash.GetEnumerator();
while(hashEn.MoveNext())
{
tmpInt = (int)hashEn.Value;
tmpInt--;
myHash[hashEn.key] = tmpInt;
}
}
How do I get around this?
int tmpInt;
Hashtable myHash = new Hashtable();
populate(myHash);
lock(myHash.SyncRoot)
{
IDictionaryEnumerator hashEn = myHash.GetEnumerator();
while(hashEn.MoveNext())
{
tmpInt = (int)hashEn.Value;
tmpInt--;
myHash[hashEn.key] = tmpInt;
}
}