How do I retrieve data from a & lt; & gt; containing class objects

advertisements

I want to retrieve data from a list I created that contains class objects via a foreach but I'm not able to. Can somebody please tell me what's missing in my code?

I have a class Recipes.cs that contains the 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 i baconfedt", 680);
        Recipe opskrift3 = new Recipe("Tun i 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() from another class calculator.cs and the code looks like this:

private int FindRecipes()
{
    List<Recipe> Rlist = new List<Recipe>();

    // CREATE THE CLASS AND ADD DATA TO THE LIST
    Recipes r = new Recipes();
    Rlist = r.CreateRecipeList();

    int test = 0; // used only for test purposes          

    foreach(var rec in Rlist)
    {
        rec.????
        test++;
    }

    return test;
}

I would presume that I should be able to dot my way into rec."the class object name"."the value"
But nothing happens!. All I get is the option to rec.Equals, rec.GetHashcod ect. which is clearly wrong.

For the record I have also tried:

foreach(Recipe rec in Rlist)
{
    rec.????
    test++;
}

But that doesn't work either. The Int test are only there for test purposes.. and it return 3.. so the list does contain the correct information.


Please show us the code for the Recipe class. Besides that, you're most of the way there...

    foreach(Recipe rec in Rlist)
    {
         string str = rec.<PropertyName>;
    }