c# - Adding collection to listbox -
c# - Adding collection to listbox -
i'm trying create user able add together name , ingredient listbox.
here brief description of relevant classes:
recipeform form.
recipemanager collection manager class recipe
recipe "content" class fieldsname , ingredients are
listmanageris generic base of operations class in manager classrecipemanager inherits from.
i want user input name , ingredients of recipe object.
and save object in object of recipemanager in recipeform class.
the list of recipe objects saved in collection (m_foodmanager in code below) supposed displayed in listbox in recipeform
here how tried it:
the form foodregister:
public partial class foodregister : form { private recipe m_food = new recipe(); private recipemanager m_foodmanager = new recipemanager(); public foodregister() { initializecomponent(); } private void readvalues() { m_food.name = nametxt.text; m_food.ingredients.add(ingredienttxt.text); } private void updateresults() { resultlistlst.items.clear(); //erase current list //get 1 elemnet @ time manager, , phone call //tostring method info - send listbox (int index = 0; index < m_foodmanager.count; index++) { recipe recipe = m_foodmanager.getat(index); //adds list. resultlistlst.items.add(recipe); } } private void addbtn_click(object sender, eventargs e) { recipe recept = new recipe(); m_foodmanager.add(recept); readvalues(); messagebox.show(m_food.ingredients.tostring()); updateresults(); } } recipe class
public class recipe { private listmanager<string> m_ingredients = new listmanager<string>(); private string name; public string name { { homecoming name; } set { name = value; } } public listmanager<string> ingredients { { homecoming m_ingredients; } } public override string tostring() { homecoming string.format("{0} {1}",this.name, this.ingredients); } } the recipemanagerclass (this 1 empty because inherits methods listmanager.:
public class recipemanager : listmanager<recipe> { public recipemanager() { } } the listmanager class:
public class listmanager<t> : ilistmanager<t> { protected list<t> m_list; public listmanager() { m_list = new list<t>(); } public int count { { homecoming m_list.count; } } public void add(t atype) { m_list.add(atype); } public void deleteat(int anindex) { m_list.removeat(anindex); } public bool checkindex(int index) { homecoming ((index >= 0) && (index < m_list.count)); } public t getat(int anindex) { if (checkindex(anindex)) homecoming m_list[anindex]; else homecoming default(t); } } the problem when i'm clicking add together button after inputting name , ingredients, listbox displays:
[namespace].listmanager`1[system.string]
recipe recept = new recipe(); m_foodmanager.add(recept);
your recept object has no value.
my solution:
-in recipe class: add together constructor recipe object
public recipe(string name){ this.name = name; } -in recipemanager class: add together method:
public list<recipe> addlist(recipe r){ homecoming m_list; } and alter "t" "recipe" object.
-in foodregister class: add together code method
private void addbtn_click(object sender, eventargs e){ recipe rc = new recipe(nametxt.text); listmanager lm = new listmanager(); lm.add(rc); (int index = 0; index < lm.count; index++) { recipe recipe = lm.getat(index); resultlistlst.items.add(recipe.name); } } hope that's useful. allow me know when it's done.
c# visual-studio-2013
Comments
Post a Comment