c# - Textfile to class to Data Dictionary -


i'm trying make textfile goes class , stores object data dictionary , create gui allows me add, edit , remove etc.

my text file in following syntax:

country, gdp growth, inflation, trade balance, hdi ranking, main trade partners

examples:

  • usa,1.8,2,-3.1,4,[canada;uk;brazil]
  • canada,1.9,2.2,-2,6,[usa;china]

anyway before create gui, trying make work in console first. countries come in console using step in debugger, object array , data dictionary (if have created data dictionary correctly) seem storing though when goes on next country overwrites previous one. how can make countries stored instead of one. if makes sense appreciated.

my code:

 class program {     static void main(string[] args)     {          const int max_lines_file = 50000;         string[] alllines = new string[max_lines_file];         int = 0;         //reads bin/debug subdirectory of project directory         alllines = file.readalllines(@"c:\users\jack\documents\countries.csv");         country[] newcountry = new country[30];          foreach (string line in alllines)         {             if (line.startswith("country")) //found first line - headers             {                 headers = line.split(',');             }             else             {                string[] columns = line.split(',');                       newcountry[i] = new country();                  newcountry[i].country=(columns[0]);                  newcountry[i].gdp=(columns[1]);                  newcountry[i].inflation=(columns[2]);                  newcountry[i].tb =(columns[3]);                  newcountry[i].hdi =(columns[4]);                  newcountry[i].tp = (columns[5]);                   dictionary<object, string> countrylist = new dictionary<object, string>();                 countrylist.add(newcountry[i].country, newcountry[i].gdp + "," + newcountry[i].inflation + "," + newcountry[i].tb + "," + newcountry[i].hdi + "," + newcountry[i].tp);                 i++;                 foreach (keyvaluepair<object, string> country in countrylist)                 {                     console.writeline("country = {0}, gdp = {1}",                         country.key, country.value);                 }              }             console.readkey();         }     }      public static string[] headers { get; set; } }   public class country {     public string country { get; set; }     public string gdp { get; set; }     public string inflation { get; set; }     public string tb { get; set; }     public string hdi { get; set; }     public string tp { get; set; }  } 

}

edit: moved countrylist suggested still getting same issue countrylist count staying on 1.

i have put in method in country class.

        public static void addd(country a)     {         dictionary<object, string> countrylist = new dictionary<object, string>();         countrylist.add(a.country, a.gdp);           foreach (keyvaluepair<object, string> country in countrylist)         {             console.writeline("country = {0}, gdp = {1}", country.key, country.value);         }     } 

and calling here:

            newcountry[i].tb =(columns[3]);          newcountry[i].hdi =(columns[4]);          newcountry[i].tp = (columns[5]);          country.addd(newcountry[i]); 

try code, have cleaned , added comments hard work with.

       static void main(string[] args)     {         // initalize loop iterator         int = 0;          // initalize list of country data         country[] newcountry = new country[30];          // initialize list of countries         dictionary<object, string> countrylist = new dictionary<object, string>();          // read lines file         string[] filelines = file.readalllines(@"c:\users\jack\documents\countries.csv");          // each line in file         foreach (string line in filelines)         {             // check if line header             if (line.startswith("country"))             {                 headers = line.split(',');             }             else             {                 // split text data                 string[] columns = line.split(',');                  // initalize new country                 newcountry[i] = new country                 {                     country = columns[0],                     gdp = columns[1],                     inflation = columns[2],                     tb = columns[3],                     hdi = columns[4],                     tp = columns[5]                 };                  countrylist.add(newcountry[i].country, string.format("{0},{1},{2},{3},{4}", newcountry[i].gdp, newcountry[i].inflation, newcountry[i].tb, newcountry[i].hdi, newcountry[i].tp));                 i++;             }         }          // process done show data         foreach (keyvaluepair<object, string> country in countrylist)         {             console.writeline("country = {0}, gdp = {1}", country.key, country.value);         }          // wait user key         console.readkey();     }      public static string[] headers { get; set; }      public class country     {         public string country { get; set; }         public string gdp { get; set; }         public string inflation { get; set; }         public string tb { get; set; }         public string hdi { get; set; }         public string tp { get; set; }      } 

further update why use country collection? below better approach

static void main(string[] args) {     // initalize list of country data     list<country> countrylist = new list<country>();      // read lines file     string[] filelines = file.readalllines(@"countries.csv");      // each line in file     foreach (string line in filelines)     {         // check if line header         if (line.startswith("country"))         {             headers = line.split(',');         }         else         {             // split text data             string[] columns = line.split(',');              // initalize new country             country filecountry = new country             {                 country = columns[0],                 gdp = columns[1],                 inflation = columns[2],                 tb = columns[3],                 hdi = columns[4],                 tp = columns[5]             };              countrylist.add(filecountry);         }     }      // process done show data     foreach (country country in countrylist)     {         console.writeline("country = {0}, gdp = {1}", country.country, country.gdp);     }      // wait user key     console.readkey(); }  public static string[] headers { get; set; }  public class country {     public string country { get; set; }     public string gdp { get; set; }     public string inflation { get; set; }     public string tb { get; set; }     public string hdi { get; set; }     public string tp { get; set; }  } 

second update per question, listbox great way start working win ui controls have added example below works. add button called button1 , listbox control called

// list holding country data         private list<country> countrylist;          /// <summary>         /// handle button click         /// </summary>         /// <param name="sender">see msdn</param>         /// <param name="e">see msdn</param>         private void button1_click(object sender, eventargs e)         {             // initalize list of country data             this.countrylist = new list<country>();              // set display text display property of country class             this.listbox1.displaymember = "display";              // set value of list box item current country             this.listbox1.valuemember = "country";              // populate data             this.populatedata();              // set list box datasource             this.listbox1.datasource = this.countrylist;         }          /// <summary>         /// gets country data csv file         /// </summary>         private void populatedata()         {             // read lines file             string[] filelines = file.readalllines(@"countries.csv");              // each line in file             foreach (string line in filelines)             {                 // check if line header                 if (line.startswith("country"))                 {                     headers = line.split(',');                 }                 else                 {                     // split text data                     string[] columns = line.split(',');                      // initalize new country                     country filecountry = new country                     {                         country = columns[0],                         gdp = columns[1],                         inflation = columns[2],                         tb = columns[3],                         hdi = columns[4],                         tp = columns[5]                     };                      this.countrylist.add(filecountry);                 }             }              // process done show data             foreach (country country in this.countrylist)             {                 console.writeline(country.display);             }         }          public static string[] headers { get; set; }          public class country         {             public string country { get; set; }             public string gdp { get; set; }             public string inflation { get; set; }             public string tb { get; set; }             public string hdi { get; set; }             public string tp { get; set; }              /// <summary>             /// formats display string country              /// </summary>             public string display             {                                 {                     return string.format("country = {0}, gdp = {1}", this.country, this.gdp);                 }             }         } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -