Home > Enterprise >  How to use the properties of a class in the same class like they have values in C#
How to use the properties of a class in the same class like they have values in C#

Time:05-22

I am trying to use a property of a class in the same class to compare with another value. However, this property is still empty because I do not have an instance of that class. See the example below.

public Zoo(string name, int capacity)
{
    Name = name;
    Capacity = capacity;    
}
public Zoo()
{
    AnimalsWhichCouldNotBeModified = new List<Animal>();
}
public List<Animal> AnimalsWhichCouldNotBeModified { get; set; }
public string  Name { get; set; }
public int Capacity { get; set; }

public string  AddAnimal(Animal animal)
{
    // Here I have tried to use the property Capacity.
    // I have also tried to use "capacity" from the constructor.
    // I have also tried to create an instance of the Zoo class in the Zoo class, but it is still empty, so I am not sure if I can do that.  
    if (Capacity < AnimalsWhichCouldNotBeModified.Count)
    {
        return "The zoo is full.";
    }

How can I get the capacity (that is still not instanced and it is null) to be used in the example so I can check if the animals are more than the zoo capacity?

CodePudding user response:

In your second constructor (public Zoo() that doesn't require any other parameters), you could just set your Capacity = 0.

Each time an instance is created, you're either going to use the first constructor, where you need to manually provide a Capacity, or the second constructor that is automatically going to set the Capacity to value 0.

public Zoo(string name, int capacity)
{
    Name = name;
    Capacity = capacity;    
}
public Zoo()
{
    Capacity = 0;
    AnimalsWhichCouldNotBeModified = new List<Animal>();
}
public List<Animal> AnimalsWhichCouldNotBeModified { get; set; }
public string  Name { get; set; }
public int Capacity { get; set; }
public string  AddAnimal(Animal animal)
{
    // Here I have tried to use the property Capacity.
    // I have also tried to use "capacity" from the constructor.
    // I have also tried to create an instance of the Zoo class in the Zoo class, but it is still empty, so I am not sure if I can do that.  
    if (Capacity < AnimalsWhichCouldNotBeModified.Count)
    {
        return "The zoo is full.";
    }
}

CodePudding user response:

The problem is you don't initialize AnimalsWhichCouldNotBeModified in the non-default constructor. You can either add the initialization into the non-default constructor:

public Zoo(string name, int capacity)
{
    Name = name;
    Capacity = capacity;    
    AnimalsWhichCouldNotBeModified = new List<Animal>();
}

or call the default one from the non-default:

public Zoo(string name, int capacity) : this()
{
    Name = name;
    Capacity = capacity;    
}

public Zoo()
{
    AnimalsWhichCouldNotBeModified = new List<Animal>();
}

or, third option, provide an initializer in the property's declaration:

public List<Animal> AnimalsWhichCouldNotBeModified { get; set; } = new List<Animal>();

I'd personally prefer this third option unless I needed a more complex initialization code in the constructor.

Note that when Capacity is not initialized, it will be 0 by default. This is because the underlying (compiler-generated) backing field is initialized to 0. The same holds for AnimalsWhichCouldNotBeModified and Name which are null by default.

  • Related