2+Enumerator[System.String,System.Int32].VerifyState () error

Hi, when i do the following on a dictionary,

foreach (KeyValuePair<string, int> pair in mTransitions)
    mTransitions[pair.Key] = Mathf.RoundToInt(pair.Value / totalWeight);

i get the error of;

InvalidOperationException: out of sync
System.Collections.Generic.Dictionary`2+Enumerator[System.String,System.Int32].VerifyState ()

According to a forum post in go.mono.com;

So, how am i supposed to loop through my dictionary and write on the values?

Well, i ended up using 2 dictionaries and copying one on an other. But it doesnt make sense that i cant write on a dictionary value on a foreach loop.

You cant change the content of an enumeration while enumerating the enumeration, foreach does enumerate an enumeration (nice). The reason is, if you change the content the enumerator which is used by foreach will be invalid.

You can fix this by creating an isolated copy of your enumeration and iterate over these:

foreach (KeyValuePair<string, int> pair in mTransitions.ToArray())
  mTransitions[pair.Key] = Mathf.RoundToInt(pair.Value / totalWeight);[FONT=Helvetica]
[/FONT]

Or you just use the Keys attribute from your Dictionary:

foreach (string key in mTransitions.Keys)
 mTransitions[key] = Mathf.RoundToInt(mTransitions[key] / totalWeight);

Yeah but both of your ways give the same out of sync error.
Anyways, i ended up making up a new dictionary and copying stuff back and forth. And it works for the case.

Oh, I see that the second example is indeeed faulted, but the first one should never give an out of sync error as it works on a completely different enumeration and does never change this enumeration.