Regarding List<> declaration in C# -


i'm trying learn list<> in c#. made test program takes result of 3 textboxes , inputs in multiline textbox, after put them in list (to later save list file).

here class declaration:

public class film     {         public film (string num, string title, string categ)         {             this.numero = num;             this.titre = title;             this.categorie = categ;         }          public string numero;         public string titre;         public string categorie;      } 

now instantiate list:

list<film> listefilms = new list<film>(); 

and here event:

private void btsavemovie_click(object sender, eventargs e)     {         var mymovie = new film(txtnum.text, txttitre.text, cbcateg.text);          listefilms.add(mymovie);         foreach (film x in listefilms)         {             txtaffichage.appendtext(x.tostring());         }     } 

now when run, written in text box is:

test_1.form1+film 

what did wrong?

you have override tostring() method in film class declaration.otherwise returns type name.

example:

public class film {     public film(string num, string title, string categ)     {          this.numero = num;          this.titre = title;          this.categorie = categ;     }      public string numero;     public string titre;     public string categorie;      public override string tostring()     {         return numero.tostring() + " " + titre.tostring() + " " + categorie.tostring();     } } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -