I define a value in a class, when I instantiate it, the value is another

1

This is the code where I defined a class Animal and a creator method that asks the species of the animal and defines its state as alive:

        public class Animal
    {
        public bool estaVivo, usaDrogas = false;
        string Especie;
        public Animal()
        {
            bool estaVivo = true;
            Console.WriteLine("Qual a espécie desse animal?");
            Especie = Console.ReadLine();
            Console.WriteLine(estaVivo);//debug
        }

Next, I have the home class, which inherits from Animal

  public class Domestic : Animal
  {
        public Domestico()
        {
            Console.WriteLine("Qual o nome do seu bicho de estimação?");
            Nome = Console.ReadLine();
            Console.WriteLine("Qual a idade do seu bicho de estimação? (NUMERIC)");
            Idade = Convert.ToInt32(Console.ReadLine());
        }
  }

Finally the main method where you instantiate the object:

        static void Main(string[] args)
    {
        Domestico bicho = new Domestico();

        Console.WriteLine(bicho.estaVivo);//debug


        Console.ReadKey();
    }

The question is, why when the constructor method of the Animal class is called the value of this alive is True , as intended, however when instantiating the derived class Domestico the value becomes False also I can not change its value).

    
asked by anonymous 27.09.2015 / 03:37

2 answers

2

You have declared the member of class Animal as bool and since you have not set any value for it, the default value of this type is false . So there's nothing wrong.

Certainly his intention was not this. And you think you've initialized this member inside the constructor. But he did not do that.

Within the constructor, a local variable of the same name and type was declared, and a value of true was assigned to it. The variable was not used and discarded (even used for debugar , but not the normal method).

The best solution is to not declare the variable, ie just type the variable and assign the value to the member instead of a local variable.

If you want to be explicit and avoid confusion use this.estaVivo . This ensures that you are referring unambiguously to the member of the class and not to the variable - which should not even exist in the method.

public class Animal {
    public bool estaVivo, usaDrogas = false;
    string Especie;
    public Animal() {
        estaVivo = true;
        Console.WriteLine("Qual a espécie desse animal?");
        Especie = Console.ReadLine();
        Console.WriteLine(estaVivo);//debug
    }
}

See working on dotNetFiddle .

Note that if you had initialized the member with true - and in this case I think you should have done it - nor did you have to do this in the constructor. To be honest this class should not have a constructor . The only thing the builder is doing, it should not be inside a constructor. Maybe it should not even be in this class if you want to draw classes the right way .

    
27.09.2015 / 03:43
0

You are overwriting the constructor, and the default value is false

    public Domestico()
    {
        Console.WriteLine("Qual o nome do seu bicho de estimação?");
        Nome = Console.ReadLine();
        Console.WriteLine("Qual a idade do seu bicho de estimação? (NUMERIC)");
        Idade = Convert.ToInt32(Console.ReadLine());

        //Nesse bloco voce sobrescreveu o construtor da classe pai.
    }
    
27.09.2015 / 03:48