How do I retrieve data from a list<> containing class objects

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.

Answers


You need to set the proper access modifiers for the members in your Recipe class.

public : Access is not restricted.

protected : Access is limited to the containing class or types derived from the containing class.

Internal : Access is limited to the current assembly.

protected internal: Access is limited to the current assembly or types derived from the containing class.

private : Access is limited to the containing type.

By default, the members of your Recipe class will have the private access modifier.

string _oveskrift;
int _recipe_id;
string _opskrift;
int _kcal;

is:

private string _oveskrift;
private int _recipe_id;
private string _opskrift;
private int _kcal;

Maybe you want to modify your member access as follows, in order to set the values of the members only inside the class code. Any attempt to set their values outside the Recipe class will fail, as the set is private. The get remains public, which makes the value available for reading.

public class Recipe
{
    string _oveskrift;
    int _recipe_id;
    string _opskrift;
    int _kcal;

    public string Oveskrift
    {
        get
        {
            return _oveskrift;
        }
        private set
        {
            _oveskrift=value;
        }
    }

    public int RecipeId
    {
        get
        {
            return _recipe_id;
        }
        private set
        {
            _recipe_id = value;
        }
    }

    public string Opskrift
    {
        get
        {
            return _opskrift;
        }
        private set
        {
            _opskrift = value;
        }
    }

    public int Kcal
    {
        get
        {
            return _kcal;
        }
        private set
        {
            _kcal = value;
        }
    }

    public Recipe(string overskrift, int recipe_id, string opskrift, int kcal)
    {
        _oveskrift = overskrift;
        _recipe_id = recipe_id;
        _opskrift = opskrift;
        _kcal = kcal;
    }
}

Also, please read as soon as possible the following MSDN article: Capitalization Conventions. And also, this one: C# Coding Conventions (C# Programming Guide).


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>;
    }

Need Your Help

How to receive response on client side?

javascript jquery html xmlhttprequest

I have this form to upload an xml file to server, I am using fiddler to monitor each req and resp. So the server sends me a small xml and i would like to receive it in my javascript as XMLHttpRequest

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.