-1

I am making a program for a school assignment about storing a list of animals observed (I am not asking about help with the program, just something interesting I happened to stumble upon).

I wanted to make a list of animals in the animals class and thought it would be really easy and convenient to add the created animal to the list at the end of the constructor, which seemed to work out well until I ran the code.

I should have figured, but the object value is null as it is still within the constructor and (I suppose) have not in fact been created yet.

    abstract class Animal
    {
        private static List<Animal> allAnimals;
        private static int amountOfAnimals;
        private int height;
        private double weight;

        public Animal(int height, double weight)
        {
            this.height = height;
            this.weight = weight;
            amountOfAnimals++;
            allAnimals.Add(this);
        }
    }

//and later, when I use it in the "Program" class:
//(animalType, width and height are all user inputs)

        switch (animalType)
        {
            case "lion":
                new Lion(height, weight);
                break;
            case "leopard":
                new Leopard(height, weight);
                break;
            case "cheetah":
                new Cheetah(height, weight);
                break;
            default:
                Console.WriteLine("Animal type does not exist.");
                continue; //it's in a loop, so break would break that
        }

I would like to know if there is any way to do what I am trying to do, even though Animal.allAnimals.Add(new Lion(height, weight)) would work. That would also require me to have a public setter on the list, unless I move it to the program class.

Just thought it was an interesting concept and would like to know if it is possible in any way, even though I highly suspect it would be considered bad practice.

brandwegg
  • 42
  • 7
  • 1
    *"That would also require me to have a public setter on the list"* - no, just a public *getter*. – madreflection Oct 24 '19 at 16:21
  • 3
    `private static List allAnimals = new List` and it should work. You don't need `amount` because you have `allAnimals.Count`. –  Oct 24 '19 at 16:22
  • Doing `new Lion(...);` (etc.) like that is screaming ***red flag!*** You're not doing anything with it in the instantion context. The fact that it gets tracked by the class itself (in the static list) is what's really standing out. That "action at a distance" is will be difficult to reason about in real-world applications. Best not to get in that habit. – madreflection Oct 24 '19 at 16:44
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). The exact issue you are facing is in this answer. – Trevor Oct 24 '19 at 16:54

1 Answers1

2

You need to instantiate the list first.

private static List<Animal> allAnimals = new List<Animal>();
gabriel.hayes
  • 2,267
  • 12
  • 15