Static Variables In Inherited Classes

B

Bryan Green

So I'm working on a project for a C# class I'm taking, where I need to
keep some running totals via static variables. I need three classes
for three different types of objects. The base class and inherited
classes are all identical. I need to refer to the static variables in
each class, and each must maintain its own values for each static
variable.

Now: when I inherit my base classes, the static variables in all my
classes contain the same values. BUT: if I copy and paste the code
from my base class into two new renamed classes (versus using
inheritance), each class maintains its own distinct values in all the
static variables. In other words, Class1.StaticVariable is different
from Class2.StaticVariable, etc.

What I need to do (for this assignment) is to see this behavior using
inheritance instead. But as I stated earlier, when I inherit the base
class, Class1.StaticVariable = Class2.StaticVariable =
Class3.StaticVariable.

And, by the way, these variables values' are being incremented with
each classes contructor (and not a class method).

Thanks in advance for any advice!
Bryan
 
J

Jon Skeet [C# MVP]

Bryan Green said:
So I'm working on a project for a C# class I'm taking, where I need to
keep some running totals via static variables. I need three classes
for three different types of objects. The base class and inherited
classes are all identical. I need to refer to the static variables in
each class, and each must maintain its own values for each static
variable.

Now: when I inherit my base classes, the static variables in all my
classes contain the same values. BUT: if I copy and paste the code
from my base class into two new renamed classes (versus using
inheritance), each class maintains its own distinct values in all the
static variables. In other words, Class1.StaticVariable is different
from Class2.StaticVariable, etc.

What I need to do (for this assignment) is to see this behavior using
inheritance instead. But as I stated earlier, when I inherit the base
class, Class1.StaticVariable = Class2.StaticVariable =
Class3.StaticVariable.

And, by the way, these variables values' are being incremented with
each classes contructor (and not a class method).

You can't. The variables belong to the type they're declared in. If you
want three different static variables, you need to *declare* three
different static variables. For instance, you could have one of these
static variables per class.

You may well wish to consider redesigning your class hierarchy. It
sounds like you may want to use composition rather than inheritance.
 
S

Scott Coonce

Access the static variables using properties. I'd follow Jon's advice-- I
didn't read super carefully your entire post, just enough to see one
solution. Jon is not often wrong.

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top