Static variables and inheritance
Hello,
I ran into a problem I didn't realize before:
- I've got a a Parent class declaring a static variable. The parent class
also provides a method to return that static variable.
- I've got two child classes (ChildA and ChildB) inheriting from the Parent
class. these also initialize the static variable in the constructor.
Look at the code below.
public abstract class Parent {
protected static string message;
public Parent () { }
public string GetMessage() {return message;}
}
class ChildA : Parent {
public ChildA () {message = "Hello from ChildA";}
}
class ChildB : Parent {
public ChildB () {message = "Hello from ChildB";}
}
When I run :
ChildA testA = new ChildA();
ChildB testB = new ChildB();
Console.Write(" {0} - {1}, testA.GetMessage(), testB.GetMessage());
It returns "Hello from ClassB - Hello from ClassB"
My goal however was to provide an abstract Parent class. Every child class
would initialize its own static variable, and would use the Parent method
'GetMessage' to return it. My intuitive way of solving this would be to
declare the static variable as abstract. However, this isn't allowed in C#.
I can't find an elegant way to solve this problem without having to declare
the static variable and implement the 'GetMessage()' method in every
subclass.
Any clues?
[1492 byte] By [
grmAbay] at [2007-11-9 18:50:17]

# 1 Re: Static variables and inheritance
"grmAbay" <grmAbay@hotmail.com> wrote:
>Hello,
>
>I ran into a problem I didn't realize before:
>
>- I've got a a Parent class declaring a static variable. The parent class
>also provides a method to return that static variable.
>- I've got two child classes (ChildA and ChildB) inheriting from the Parent
>class. these also initialize the static variable in the constructor.
>Look at the code below.
>public abstract class Parent {
> protected static string message;
> public Parent () { }
> public string GetMessage() {return message;}
>}
>
>class ChildA : Parent {
> public ChildA () {message = "Hello from ChildA";}
>}
>
>class ChildB : Parent {
> public ChildB () {message = "Hello from ChildB";}
>}
>
>When I run :
> ChildA testA = new ChildA();
> ChildB testB = new ChildB();
> Console.Write(" {0} - {1}, testA.GetMessage(), testB.GetMessage());
> It returns "Hello from ClassB - Hello from ClassB"
>
>My goal however was to provide an abstract Parent class. Every child class
>would initialize its own static variable, and would use the Parent method
>'GetMessage' to return it. My intuitive way of solving this would be to
>declare the static variable as abstract. However, this isn't allowed in
C#.
>
>I can't find an elegant way to solve this problem without having to declare
>the static variable and implement the 'GetMessage()' method in every
>subclass.
>
>Any clues?
>
>
It sounds like you don't really want a static variable if you're expecting
it to be assigned to by instance...
Dave at 2007-11-11 21:58:10 >

# 2 Re: Static variables and inheritance
"grmAbay" <grmAbay@hotmail.com> wrote:
>Hello,
>
>I ran into a problem I didn't realize before:
>
>- I've got a a Parent class declaring a static variable. The parent class
>also provides a method to return that static variable.
>- I've got two child classes (ChildA and ChildB) inheriting from the Parent
>class. these also initialize the static variable in the constructor.
>Look at the code below.
>public abstract class Parent {
> protected static string message;
> public Parent () { }
> public string GetMessage() {return message;}
>}
>
>class ChildA : Parent {
> public ChildA () {message = "Hello from ChildA";}
>}
>
>class ChildB : Parent {
> public ChildB () {message = "Hello from ChildB";}
>}
>
>When I run :
> ChildA testA = new ChildA();
> ChildB testB = new ChildB();
> Console.Write(" {0} - {1}, testA.GetMessage(), testB.GetMessage());
> It returns "Hello from ClassB - Hello from ClassB"
>
>My goal however was to provide an abstract Parent class. Every child class
>would initialize its own static variable, and would use the Parent method
>'GetMessage' to return it. My intuitive way of solving this would be to
>declare the static variable as abstract. However, this isn't allowed in
C#.
>
>I can't find an elegant way to solve this problem without having to declare
>the static variable and implement the 'GetMessage()' method in every
>subclass.
>
>Any clues?
>
>
I agree with Dave. You don't want message to be static. The whole point
of a static is to have a single instance shared by all instantiations of
the class, including derived (child) objects.
