-2

I have A Parent Class Animal And Child Class Dog.

Public Class Animal
{
    public int Height = 0;
}

Public Class Dog : Animal
{
    public int AnimalHeight()
    {
        return this.Height;
    }
}

//Executing the Code in Main Method
Public Static Void main(stirng [] args)
{
    Animal animal = new Animal();
    animal.Height = 100;

    Dog dog = new Dog();
    var heights = dog.AnimalHeight(); // why I didn't get 100 in this variable????
}

You can see I have Assign 100 Height in parent why I didn't get 100 in this variable 'heights'? .......................................... I just want to achieve that when I set variable on one side and it sets on all child Classes simple.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Dev
  • 3
  • 4
  • 4
    Every time you use `new`, you're creating a new object. It doesn't share any (non-`static`) data with any previously created object. – Damien_The_Unbeliever Jan 11 '21 at 09:14
  • 2
    Don't write code in Word. Also, you have `new` twice. Do you understand what `new` does? – CodeCaster Jan 11 '21 at 09:14
  • Can u share the code where i can get 100 in Child Class – Dev Jan 11 '21 at 09:15
  • 1
    creating an instance of base-class `Animal` makes no sense, IMHO. That´s what abstract classes are for, so make `Animal` abstract. – MakePeaceGreatAgain Jan 11 '21 at 09:18
  • @Damien_The_Unbeliever Can u write a piece of code for me where u assign value to parent and get it from Child – Dev Jan 11 '21 at 09:18
  • @HimBromBeere what will happen when i create Animal a Abstract???? – Dev Jan 11 '21 at 09:19
  • 1
    you can´t create an instance of an abstract class - because it really makes no sense to create an animal without specifiying what kind of animal it actually is. So you are forced to specifiy the type at instantiation-time: `Animal dog = new Dog()`. – MakePeaceGreatAgain Jan 11 '21 at 09:20
  • when you want **all** animals to be of height 100, you should make the field static. – MakePeaceGreatAgain Jan 11 '21 at 09:42
  • @HimBromBeere Can u please write a small code for me? – Dev Jan 11 '21 at 09:52
  • @dev what code other than I wrote in my answer do you expect? – MakePeaceGreatAgain Jan 11 '21 at 10:00
  • 1
    @HimBromBeere i need a code where i change Height Onside and All Childs value Changed .. do u understand? – Dev Jan 11 '21 at 10:21
  • @Dev Does using a [static](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/static) field or property in the root class match your goal? It will be [shared among all classes](http://www.functionx.com/csharp/introduction/Lesson10.htm) in the hierarchy, between all instances of any type of the base class and any subclass. –  Jan 11 '21 at 10:29
  • @Dev Therefore is for this duplicate you ask for: [Static fields in a base class and derived classes](https://stackoverflow.com/questions/5851497/static-fields-in-a-base-class-and-derived-classes) ? –  Jan 11 '21 at 10:35
  • See my update for `static`. – MakePeaceGreatAgain Jan 11 '21 at 10:36

3 Answers3

2

Your code will compile to this:

new Animal().Height = 100;
new Dog().AnimalHeight();

So as you can see, every time you are using the new keyword, you're creating a new object. You can start reading about that here https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types

So what you need is just something like this:

Dog dog = new Dog();
dog.Height = 100;

var heights = dog.AnimalHeight(); 

With that being said, I think you don't need the base class at all!

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

Actually you have two problems here. The most obvious is that you´re creating two completely unrelated objects here - one of type Animal and one of type dog. They don´t share any members - in particular they have completely independendn Height.

So you should just create a single dog

//Executing the Code in Main Method
public static void main(stirng [] args){
    Dog dog = new Dog();
    dog.Height = 100;
    var heights = dog.AnimalHeight(); 
}

The other somehow related but not as obvious problem is more of a design-issue: your Animal-class should be abstract. This way your problem can´t occur in the first place, because you cannot write new Animal(). Practically creating an animal of unspecific type makes no sense - and that´s what abstract does.

Apart from this public fields are usually discouraged - at least for non DTO-classes. After all your code should look like this:

abstract class Animal 
{
    public int Height { get; set; } // implement getter/setter-logc when needed
}
public class Dog : Animal
{
    public int AnimalHeight()
    {
        return this.Height;
    }
}

If you want to share the Height accross all instances of animals, make the field/property static:

abstract class Animal 
{
    public static int Height { get; set; } 
}

Now you can set it anywhere in your code:

Animal.Height = 10;
Dog d = new Dog();
Console.WriteLine(d.AnimalHeight()); // prints 10
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

Because, you assigned the value 100 to the object animal. then you create a new object dog. In memory animal and dog, didn't have the same allocation, so the value of the two created objetcs is different.