Convert object string to object(class) in C# using Winforms -
here going tell working on , after there comes question. want tell guys working on.. guys can understand problem better.
thanks in advance,
i working on project. app grocery calculator. got class called artikel. class base class of other derived classes (vegetables, drinks, warmfood.. etc).
i got combobox filled data database. want add price of each product "total cost cart". base class looks this:
class artikel { // properties private string naam; private double prijs; private string winkel; // properties public string naam { { return naam; } set { naam = value; } } public double prijs { { return prijs; } set { prijs = value; } } public string winkel { { return winkel; } set { winkel = value; } } // constructor public artikel(string naam, double prijs, string winkel) { this.naam = naam; this.prijs = prijs; this.winkel = winkel; } // methods public override string tostring() { string info = "€ " + prijs; return info; }
from main form use code add combobox:
foreach(var g in database.getgroenteinfo()) { cbgroentenlidl.items.add(g.naam); }
when combobox full "vegetable" data can select item. here can add selected item "lblidlkosten" code:
lblidlkosten.items.add(cbgroentenlidl.selecteditem);
now data right want it.. here problem. want add price each product add listbox... label. basicly more product add "lblidlkosten" more costs grow in label.text. hope know mean.
i tried following 1 working:
artikellist = database.getgroenteinfo(); //foreach (artikel in artikellist) //{ // label2.text = convert.tostring(a.prijs); // label2.text = cbgroentenlidl.selecteditem; //} //foreach (artikel in lblidlkosten.items) //{ // label2.text = a.prijs.tostring(); //} //artikel artikel = (artikel)cbgroentenlidl.selecteditem; //label2.text = artikel.prijs.tostring(); foreach (artikel in lblidlkosten.items) { label2.text = convert.tostring(a.prijs); }
all these solutions won't work. because getting error: "cannot convert object string object artikel".
does has clue? simple.. cannot find , stuck last 3 days.
with kind regards
in here:
foreach(var g in database.getgroenteinfo()) { cbgroentenlidl.items.add(g.naam); }
you're adding name combo box, rather item itself. perhaps can try binding item collection combo box datasource, so:
cbgroentenlidl.datasource = database.getgroenteinfo().tolist(); cbgroentenlidl.displaymember = "naam";
then when add listbox, have object, rather name.
Comments
Post a Comment