wpf - C# class properties enumerable -


i have problem enumeration of class. searched on internet , stackoverflow, i'm afraid limited experience c# limits me recognizing exact situation , solution. code have:

public list<annotation> annotations = new list<annotation>();  public class annotation {     public annotation(int pos, int x2, int y2, string artnr)     {         this.artnr = artnr;         this.pos = pos;         this.x2 = x2;         this.y2 = y2;     }     public string artnr;     public int pos;     public int x2;     public int y2;   }  public void add_anno(string artnr, int x2, int y2) {     annotations.add(new annotation(0,x2,y2,artnr)); } 

after adding sever annotations want display them in wpf canvas object. problem can't loop through items in list. question method should use enumeration , how apply it? list contains both integers , string. tried use:

system.collections.ienumerator ie = annotations.getenumerator(); while(ie.movenext()) {     annotation test = ie.gettype().getproperties(); } 

meanwhile i'm looking @ reflection in "microsoft press exam 7--483" see if solution.

thanks.

you need use foreach, iterate each 1 of elements in list. there's no need use reflection:

foreach (var annotation in annotations) {     int position = annotation.pos;     string atrnr = annotation.artnr; } 

as side-note, advise @ c# naming conventions, , perhaps study properties

a quick refactoring this:

public class annotation {     public annotation(int pos, int x2, int y2, string artnr)     {         this.artnr = artnr;         this.pos = pos;         this.x2 = x2;         this.y2 = y2;     }      public string artnr { get; private set; }     public int pos { get; private set; }     public int x2 { get; private set; }     public int y2 { get; private set; } } 

if you're using c# 6, can drop private set.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -