Adding a collection or list to an object

Posted on 2007-07-23
Last Modified: 2010-04-16
I am creating an object and would like to add a collection of sorts to it.  I have a database table that looks like the following (for example):

Person  Pet  Role
Jim         Cat    5
Jim         Dog   3
Sue        Bird   2
Sue        Fish   3

My object will be associated with a person, like "Jim" in the above.  I will call this the "Person" object.  Is there a way to add a list of the animals, along with their role values into this object, so I can do something like this:

Peson.Name                      //Would return Jim
Person.Animals[1].Name   //Would return Dog
Person.Animals[1].Role    //Would return 3

and also to structure the Person class so it can do loops:

foreach Animal in Person.Animals

Can someone walk me through how to do this?  Thanks.

I am guessing I first need to create the Animal object with the name and role method.  But I am unsure how to add this to the Person class so it can be used like the example above.
Question by:dentyne
    LVL 3

    Accepted Solution

    In the Person class:

    add a private member:

    Private List<Animal> _animals;

    Instantiate it in the constructor:

    _animals = new List<Animal>();

    and add a property:

    public List<Animal> Animals {
        get { return _animals; }

    then you can do:

    Person.Animals[0].Name = "Hamster"  and such.

    LVL 3

    Expert Comment

    ehh ofcourse first add a new Animal to the list by:

    Animal bird = new Animal();
    bird.Name = "Bird";
    bird.Role = 21;

    and stuff like that.

    LVL 1

    Author Comment

    Great thanks!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now