c# 4.0 - Trying to enumerate over a deserialized list throws an exception -


i know exception has been addressed billion times situation different (i think).

anyway, using protobuf - net save , load objects. i've got list of objects i'm trying deserialize keeps breaking saying (here comes):

collection modified; enumeration operation may not execute. 

again, i've seen question asked 50 times here i'm sorry 50 times here's code:

public void load(){             using (var file =                  file.exists(application.startuppath + @"\testfile.scn") ?                  file.openread("testfile.scn") :                  null){                 if (file != null){                     this._tlpgrid.controls.clear();                     this.scenes = serializer.deserialize<list<graphicspanel>>(file);                     foreach(graphicspanel gp in this._lgpscenes)                         this.addscene(gp);                 }             } } 

why throwing exception , proper way go if i'm doing wrong?

edit: indicated me addscene method modifying list. correct: original:

    public void addscene(graphicspanel scene){         this._tlpgrid.controls.add(scene);         this.scenes.add(scene);     } 

modified:

    public void addscene(graphicspanel scene){         this._tlpgrid.controls.add(scene);         if (!this.scenes.contains(scene))             this.scenes.add(scene);     } 

the question has been answered much.

so evidently issue method calling modify list modifying list while iterating through it. should have been obvious missed it. thank pointing out me, lee. code method throwing exception:

public void load(){             using (var file =                  file.exists(application.startuppath + @"\testfile.scn") ?                  file.openread("testfile.scn") :                  null){                 if (file != null){                     this._tlpgrid.controls.clear();                     this.scenes = serializer.deserialize<list<graphicspanel>>(file);                     foreach(graphicspanel gp in this._lgpscenes)                         this.addscene(gp);                 }             } } 

and method causing issue (before):

public void addscene(graphicspanel scene){     this._tlpgrid.controls.add(scene);     this.scenes.add(scene); } 

and after:

public void addscene(graphicspanel scene){     this._tlpgrid.controls.add(scene);     if (!this.scenes.contains(scene))         this.scenes.add(scene); } 

again patiently pointing out obvious.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -