I am trying to iterate over a dictionary containing objects as a value:
foreach (KeyValuePair<int, CMapPool_Entry> Entry in MapPool) { this.SendConsoleMessage(Entry.Value.Map); }
Below you can see the CMapPool_Entry class
public class CMapPool_Entry { public string Map; public string Mode; public int Rounds; public int Index; public int Votes; public bool Nominated; public string Nominator; public CMapPool_Entry(string map, string mode, int rounds, int index, string Nominator_LeaveEmptyStringIfNone) { this.Map = map; this.Mode = mode; this.Rounds = rounds; this.Index = index;
Here you can also see the SendConsoleMessage method:
private void SendConsoleMessage(string message) { this.ExecuteCommand("procon.protected.pluginconsole.write", String.Format("{0}", message)); }
It seems to me that this will work, I read about how editing the values โโfrom the dictionary inside foreach will give the following error: "The collection has been changed, the enumeration operation cannot be performed."
But why am I getting this error? I am not editing any values, am I just reading them correctly? It works fine if CObject was a string or int instead, but if it is an object, it goes out. What am I doing wrong, what should I do?
EDIT: after further debugging, I noticed that Entry.Key is fine, but as soon as I touch Entry.Value, I get errors. For some reason, I accidentally got two different errors:
- "The collection has been modified; an enumeration operation cannot be performed.
- "This key is not in the dictionary.
Any ideas? Or enumerating dictionaries with objects as values โโjust doesn't work?