473,785 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.StaticVa riable is different
from Class2.StaticVa riable, 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.StaticVa riable = Class2.StaticVa riable =
Class3.StaticVa riable.

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 10765
Think about making the static variables _less_ public.

Scott
"Bryan Green" <bc*****@yahoo. com> wrote in message
news:3c******** *************** *********@4ax.c om...
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.StaticVa riable is different
from Class2.StaticVa riable, 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.StaticVa riable = Class2.StaticVa riable =
Class3.StaticVa riable.

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.StaticVa riable is different
from Class2.StaticVa riable, 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.StaticVa riable = Class2.StaticVa riable =
Class3.StaticVa riable.

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.StaticVa riable is different
from Class2.StaticVa riable, 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.StaticVa riable = Class2.StaticVa riable =
Class3.StaticVa riable.

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.co m>
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.c om...
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.StaticVa riable is different
from Class2.StaticVa riable, 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.StaticVa riable = Class2.StaticVa riable =
Class3.StaticVa riable.

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
1898
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 (factory) class Fa. I wanted to make that only F can call new A
13
7735
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. They don't actually protect any encapsulation or anything, and for all the actual protection they offer, they might as well be public. For example: class B { protected:
0
1336
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 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...
19
24591
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 static so that I don't need an instance in order to call it. However, because my class model is using a common base class, I need to make it abstract too, so that inherited classes are forced to implement it. Am I doing something that can be...
4
2300
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 = 4; public void grow()
11
3846
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
5
1809
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 practices? Should I put related C functions into a class and make them static methods? Most of the C functions are in separate files with no global variables (one file of functions for producing random numbers, one file of functions for doing...
3
1934
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 ability inherit from this base class along with the other base classes they inherit from. This should work (not sure how efficient it is though) but I want to share the data structure that keeps the log messages between all the classes that's...
17
8384
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 the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.