By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,669 Members | 1,645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,669 IT Pros & Developers. It's quick & easy.

Static Variables In Inherited Classes

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Think about making the static variables _less_ public.

Scott
"Bryan Green" <bc*****@yahoo.com> wrote in message
news:3c********************************@4ax.com...
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

Nov 17 '05 #2

P: n/a
But if I make them private, they won't be accessible outside the
class...

On Wed, 26 Oct 2005 20:32:18 +0200, "Scott Coonce"
<sd******@gmail.HEY_YOU.com> wrote:
Think about making the static variables _less_ public.

Scott
"Bryan Green" <bc*****@yahoo.com> wrote in message
news:3c********************************@4ax.com.. .
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

Nov 17 '05 #3

P: n/a
Bryan Green <bc*****@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
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
"Bryan Green" <bc*****@yahoo.com> wrote in message
news:3o********************************@4ax.com...
But if I make them private, they won't be accessible outside the
class...

On Wed, 26 Oct 2005 20:32:18 +0200, "Scott Coonce"
<sd******@gmail.HEY_YOU.com> wrote:
Think about making the static variables _less_ public.

Scott
"Bryan Green" <bc*****@yahoo.com> wrote in message
news:3c********************************@4ax.com. ..
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

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.