473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Static Variables In Inherited Classes

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
4 10717
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ernst Murnleitner | last post by:
Hello Readers, Is there a way that only one class can construct a class A and its inherited classes A2, A3 etc.? I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
0
by: Bryan Green | last post by:
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...
19
by: Mike Ruane-Torr | last post by:
Why can't I have a static abstract method in C#? My intention is to have a class-level method that returns a string to supply information about inherited classes, and it is natural to make this...
4
by: shooter | last post by:
How does one make a function in a base class slect the appopriate version of an overriden static variable. Code-splination follows: public class letter { public static int PostCodeDigits =...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
5
by: Digital Puer | last post by:
Hi, I've inherited a bunch of C code that needs to be called from a C++ framework, so I thought it would be good to put these C functions into C++ classes for better organisation. What are best...
3
by: Jaco Naude | last post by:
Hi I am trying to think of an efficient way of implementing a logging system for a big program. My thoughts are to create a message logging base class and letting all objects that need logging...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.