KingBee
KingBee

Reputation: 56

Trying to learn c# lists and how to access them, also from other classes

I am new to C# and learning about List in OOP model. Im having an issue accessing a List, well when I run in the command prompt its just blank.

In the search class I am basically getting a List of all person's (I think I am) and searching for the Adults(over 20 years old). From the Main I am trying to print out the List of Adults. Its hard finding an answer for this question.

So this is the code:

class Program
{
    static void Main(string[] args)
    {
        Search s = new Search();
        List<Person> GameOfThrones = new List<Person>();

        GameOfThrones.Add(new Person("Rob Stark", 20, Gender.Male));
        GameOfThrones.Add(new Person("Sansa Stark", 16, Gender.Female));
        GameOfThrones.Add(new Person("The Mountain", 30, Gender.Transgender));
        GameOfThrones.Add(new Person("Mellisandra", 100, Gender.Female));
        GameOfThrones.Add(new Person("Ramsey Bolton", 20, Gender.Male));



        foreach(Person abc in s.getAdults())
        {
            Console.WriteLine(abc.ToString());
        }


        Console.ReadKey();
    }

}

class Search
{
    private List<Person> allPersons = new List<Person>();

    public List<Person> Apl
    {
        get { return allPersons; }
    }
    public List<Person> getAdults()
    {
        List<Person> Adults = new List<Person>();
        foreach (Person test in allPersons)
        {

            if (test.Age > 20)
            {
                Adults.Add(test);
                //Console.WriteLine(test.ToString() + "\n");
            }
        }
        return Adults;
    }

    public List<Person> allMales()
    {
        List<Person> males = new List<Person>();
        foreach(Person per in allPersons)
        {
            if(per.GenderType == Gender.Male)
            {
                males.Add(per);
            }
        }
        return males;
    }
 }

class Person
{
    public Person(string name, int age, Gender gen)
    {
        this.Name = name;
        this.Age = age;
        this.GenderType = gen;
    }
    public string Name { get; set; }
    public int Age { get; set; }
    public Gender GenderType { get; set; }

    public override string ToString()
    {
        return "Name: " + Name + "\nAge: " + Age + "\nGender: " + GenderType;
    }
}

Upvotes: 0

Views: 63

Answers (3)

Jamiec
Jamiec

Reputation: 136074

You never pass your list of allPersons to Search - it always just has an empty list!


You could pass the created List<Person> into the Search class,

class Search
{
    private List<Person> allPersons;

    public Search(List<Person> allPersons)
    {
        this.allPersons = allPersons
    }
    ....
}

and

List<Person> GameOfThrones = new List<Person>();

GameOfThrones.Add(new Person("Rob Stark", 20, Gender.Male));
GameOfThrones.Add(new Person("Sansa Stark", 16, Gender.Female));
....

Search s = new Search(GameOfThrones);

A few other words of wisdom,

This block

public List<Person> getAdults()
{
    List<Person> Adults = new List<Person>();
    foreach (Person test in allPersons)
    {

        if (test.Age > 20)
        {
            Adults.Add(test);
            //Console.WriteLine(test.ToString() + "\n");
        }
    }
    return Adults;
}

Can be done very quickly and easily with LINQ

public List<Person> getAdults()
{
     return allPersons.Where(p => p.Age > 20).ToList();
}

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You are no where setting the allPersons instance in your Search class, so it will always have Count of 0, you can add a parameter in constructor of Search class for instance like:

class Search
{
   public Search(List<Person> persons)
   {
         allPersons = persons;
   }
}

and then on calling side you will need to pass that :

    List<Person> GameOfThrones = new List<Person>();

    GameOfThrones.Add(new Person("Rob Stark", 20, Gender.Male));
    GameOfThrones.Add(new Person("Sansa Stark", 16, Gender.Female));
    GameOfThrones.Add(new Person("The Mountain", 30, Gender.Transgender));
    GameOfThrones.Add(new Person("Mellisandra", 100, Gender.Female));
    GameOfThrones.Add(new Person("Ramsey Bolton", 20, Gender.Male));

     Search s = new Search(GameOfThrones );

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66439

You've created a new List<Person> in Main that has no relation to the one in your Search class, so getAdults() is always executing on an empty collection.

Access the List<Person> inside the Search class from Main.

Search s = new Search();
s.Apl.Add(new Person("Rob Stark", 20, Gender.Male));
s.Apl.Add(new Person("Sansa Stark", 16, Gender.Female));

Additionally, in getAdults(), you could replace the code with a short LINQ statement:

public List<Person> getAdults()
{
    return allPersons.Where(p => p.Age > 20).ToList();
}

Upvotes: 0

Related Questions