c# - How do I retrieve data from a list<> containing class objects -
i want retrieve data list created contains class objects via foreach i'm not able to. can please tell me what's missing in code?
i have class recipes.cs contains following code: public class recipe {
string _oveskrift; int _recipe_id; string _opskrift; int _kcal; public recipe(string overskrift, int recipe_id, string opskrift,int kcal) { _oveskrift = overskrift; _recipe_id = recipe_id; _opskrift = opskrift; _kcal = kcal; } } public class recipes { public list<recipe> createrecipelist() { recipe opskrift1 = new recipe("cornflakes med chili",1,"4 kg cornflakes bages", 420); recipe opskrift2 = new recipe("oksemørbrad",2,"oksemørbrad steges baconfedt", 680); recipe opskrift3 = new recipe("tun vand",3,"dåsen åbnes og tunen spises", 120); list<recipe> recipelist = new list<recipe>(); recipelist.add(opskrift1); recipelist.add(opskrift2); recipelist.add(opskrift3); return recipelist; } }
i call createrecipelist() class calculator.cs , code looks this:
private int findrecipes() { list<recipe> rlist = new list<recipe>(); // create class , add data list recipes r = new recipes(); rlist = r.createrecipelist(); int test = 0; // used test purposes foreach(var rec in rlist) { rec.???? test++; } return test; }
i presume should able dot way rec."the class object name"."the value"
nothing happens!. option rec.equals, rec.gethashcod ect. wrong.
for record have tried:
foreach(recipe rec in rlist) { rec.???? test++; }
but doesn't work either. int test there test purposes.. , return 3.. list contain correct information.
please show code recipe class. besides that, you're of way there...
foreach(recipe rec in rlist) { string str = rec.<propertyname>; }
Comments
Post a Comment