I define a value in a class, when I instate the same value is another

Asked

Viewed 100 times

1

This is the code where I defined a class Animal is a breeding 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 domestic 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 I instate 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 class Animal is called the value of this living is True, as intended, but when I introduce the derived class Domestico the value becomes False (and besides I can’t change its value).

2 answers

2


You declared the class member Animal as bool and as he put no value on it, the value default of this kind is false. Then there’s nothing wrong.

Certainly your intention was not this. And you think you initialized this member within the constructor. But you didn’t do this.

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

The best solution is not to declare the variable, IE, just take its type and you will be assigning the value to the member and not to a local variable.

If you want to be explicit and avoid confusion use this.estaVivo. This ensures that you are referring unambiguously to the class member 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
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

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

  • It worked well, had not noticed that I had declared the variable within the constructor. After all, I had already seen your answer about the use of constructors, that’s when I decided to do this test, to find out how a constructor works in C#. Besides, I ended up learning to use the keyword this.

  • The this is much more than this but its main feature is to disambiguate local instance variables.

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.
    }
  • Actually, no, because my program is exactly as I posted here... The error, as pointed out by @bigown is that I declared the variable estaVivo again inside the builder.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.