c# - Enumeration error; May not execute -


collection modified; enumeration operation may not execute.

i error when run program. have dictionary , when modify can't enumerate it.

public void render() {     foreach (var pair in players ) {        main.spritebatch.drawstring(main.bigfont, pair.key, pair.value, color.white);     } } 

i tried modifying @ value or removing completely, must re-add it.

 byte[] data = client.endreceive(ar, ref ip);         string str_data = encoding.ascii.getstring(data);         string[] coords = str_data.split(";".tochararray());         if (players.containskey(coords[0])) {             players.remove(coords[0]);         }         players.add(coords[0], new vector2(int32.parse(coords[1]), int32.parse(coords[2]))); 

you can try use ordinary loop instead of foreach. way can circumvent warning, since you're not using enumeration option.

for dictionary do:

list<string/*or other datatype*/> keys = players.keys.tolist(); (int = 0; < keys.count; i++) {    main.spritebatch.drawstring(main.bigfont, keys[i], players[keys[i]], color.white); } 

does suit needs?


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -