c# - add a Number to an array of strings -


i made object player(int number, string name, string guild, int hp, int mana) , csv file i'm reading list of players.

   public static list<player> getplayerfromcsv(string file)     {         list<player> result = new list<player>();         using (streamreader sr = new streamreader(file))         {             string line = sr.readline();             if (line != null) line = sr.readline();          while (line != null)         {             player pl = addplayer(line);             if (pl != null) result.add(pl);             line = sr.readline();         }     }     return result; }  private static player addplayer(string line) {     line = line.trimend(new char[] { ';' });     string[] parts = line.split(new char[] { ';' });      if (parts.length != 5) return null;      player pl = new player(parts[0], parts[1], parts[2], parts[3], parts[4]);     return pl; }  public override string tostring() {     return number + "  -  " + name; } 

it gives me error because parts 0, 3 , 4 should strings, since it's array of strings. tried parts[0].tostring() etc, it's no help

you need parse strings:

player pl = new player(int.parse(parts[0]),                         parts[1],                         parts[2],                         int.parse(parts[3]),                         int.parse(parts[4])                       ); 

but that's bare minimum - move parsing separate statements before player construction you add error checking/validation/etc.

as stands now, if parts 0, 3, or 4 not parsable, you'll vague run-time exception no context which value causes error.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -