Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i have one class called Animal.cs and in my Program.cs i have created some animals..

class Program
    {
        static void Main(string[] args)
        {
            var AnimalList = new AnimslList();
        animalList.Add(new Animal{ Active = true, Name = "Bob", Photo = "Espanha" });
        animalList.Add(new Animal { Active = false, Name = "Robby", Photo = "Portugal" });
        animalList.Add(new Animal { Active = true, Name = "Snoop", Photo = "UK" });
        }
     }

and i have one class AnimalList.cs and in that class i added Animal list, and im getting the active animals with this code:

class AnimalList : List<Animal>
{
    public List<Animal> GetActiveAnimals()
    {
        return this.FindAll(p => p.Active);
    }
}

and my problem is in this class he called Event.cs in event.cs i want get the results from GetActiveAnimals() because i just want to get the active animals for the event can someone help me please?

class Event
    {
        private AnimalList _contestants;
    }

i should get the Active from the AnimaList in this one above. The active ones should go into _contestants

What I have tried:

I didn't know how to i really need help with this it's my last problem of this code i think

i nee to get the actives in this class in the list _contestants

class Event
{
private AnimalList _contestants;
}
Posted
Updated 18-Apr-16 23:33pm

1 solution

The problem is that your AnimalList in your Main method:
C#
static void Main(string[] args)
    {
    var AnimalList = new AnimslList();
    animalList.Add(new Animal{ Active = true, Name = "Bob", Photo = "Espanha" });
    animalList.Add(new Animal { Active = false, Name = "Robby", Photo = "Portugal" });
    animalList.Add(new Animal { Active = true, Name = "Snoop", Photo = "UK" });
    }
Is local to the function - you can't access it outside the function unless you pass it to another method.
So if you were to do this:
C#
static void Main(string[] args)
    {
    var AnimalList = new AnimslList();
    animalList.Add(new Animal{ Active = true, Name = "Bob", Photo = "Espanha" });
    animalList.Add(new Animal { Active = false, Name = "Robby", Photo = "Portugal" });
    animalList.Add(new Animal { Active = true, Name = "Snoop", Photo = "UK" });
    Event ev = new Event();
    ev.ListOfAnimalsPropertyYouNeedToAdd = AnimalList;
    ...
    }
You can then use the list you passed in to identify the active ones:
C#
foreach (Animal a in ev.GetActiveAnimals())
    {
    ...
    }
By writing the code in your Event class:
C#
public List<Animal> GetActiveAnimals()
    {
    return ListOfAnimalsYouSavedWhenYouSetThePropertyEarlier.GetActiveAnimals();
    }


(But don't call it Event - that has a specific meaning in C# that your teacher will explain later)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900