473,769 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance on static variables?

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()
{PostCodeDigits ++;}

public virtual void shrink()
{PostCodeDigits--;}

public virtual string desc()
{return PostCodeDigits. ToString();}
}
public class parcil : letter
{
public static new int PostCodeDigits = 8;

public override void shrink()
{PostCodeDigits--;}

public override string desc()
{return PostCodeDigits. ToString() + "!";}
}
Examble call:

letter a, b;
a = new letter();
b = new parcil();
Console.WriteLi ne("a = {0}, b = {1}", a.desc(), b.desc());
a.grow();
b.grow();
Console.WriteLi ne("a = {0}, b = {1}", a.desc(), b.desc());
a.shrink();
b.shrink();
Console.WriteLi ne("a = {0}, b = {1}", a.desc(), b.desc());

output:
a = 4, b = 8!
a = 6, b = 8!
a = 5, b = 7!

So shrink works, but grow only effects the base class.
It quickly becomes impractical overide all functions that asses the
overriden static feild.

Is there a way in c# to make the "grow function" work as per the shrink
function" without overriding it in all inherited classes.

Nov 17 '05 #1
4 2299
Bump,

I know this works, but is there something cleaner?

public void grow()
{
int v = (int)this.GetTy pe().GetField(" PostCodeDigits" ).GetValue(this );
v++;
this.GetType(). GetField("PostC odeDigits").Set Value(this, v);
}

Nov 17 '05 #2
you can't specify that a field can be marked as virtual\overrid e, you will
have to define a virtual property getter & setter. I think the following
code gives you what you want:

using System;
namespace test1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Letter a, b;
a = new Letter();
b = new Parcel();
Console.WriteLi ne("a = {0}, b = {1}", a.Desc(), b.Desc());
a.Grow();
b.Grow();
Console.WriteLi ne("a = {0}, b = {1}", a.Desc(), b.Desc());
a.Shrink();
b.Shrink();
Console.WriteLi ne("a = {0}, b = {1}", a.Desc(), b.Desc());
Console.ReadLin e();
}
}

public class Letter
{
private static int _postCodeDigits = 4;
public virtual int PostCodeDigits
{
get
{
return _postCodeDigits ;
}
set
{
_postCodeDigits = value;
Console.WriteLi ne("Setting post code digit length for
letter - " + value);
}
}
public void Grow()
{
this.PostCodeDi gits++;
}
public void Shrink()
{
this.PostCodeDi gits--;
}
public string Desc()
{
return PostCodeDigits. ToString() + "!";
}
}

public class Parcel : Letter
{
private static int _postCodeDigits = 8;
public override int PostCodeDigits
{
get
{
return _postCodeDigits ;
}
set
{
_postCodeDigits = value;
Console.WriteLi ne("Setting post code digit length for
parcel - " + value);
}
}
}

The out from the above code is:

a = 4!, b = 8!
Setting post code digit length for letter - 5
Setting post code digit length for parcel - 9
a = 5!, b = 9!
Setting post code digit length for letter - 4
Setting post code digit length for parcel - 8
a = 4!, b = 8!

HTH

Ollie Riches
<sh*****@farts. com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
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()
{PostCodeDigits ++;}

public virtual void shrink()
{PostCodeDigits--;}

public virtual string desc()
{return PostCodeDigits. ToString();}
}
public class parcil : letter
{
public static new int PostCodeDigits = 8;

public override void shrink()
{PostCodeDigits--;}

public override string desc()
{return PostCodeDigits. ToString() + "!";}
}
Examble call:

letter a, b;
a = new letter();
b = new parcil();
Console.WriteLi ne("a = {0}, b = {1}", a.desc(), b.desc());
a.grow();
b.grow();
Console.WriteLi ne("a = {0}, b = {1}", a.desc(), b.desc());
a.shrink();
b.shrink();
Console.WriteLi ne("a = {0}, b = {1}", a.desc(), b.desc());

output:
a = 4, b = 8!
a = 6, b = 8!
a = 5, b = 7!

So shrink works, but grow only effects the base class.
It quickly becomes impractical overide all functions that asses the
overriden static feild.

Is there a way in c# to make the "grow function" work as per the shrink
function" without overriding it in all inherited classes.

Nov 17 '05 #3
<sh*****@farts. com> wrote:
How does one make a function in a base class slect the appopriate
version of an overriden static variable.


You can't - because you can't override variables. You can only hide
them, which is why you had to use the "new" keyword rather than
"override".

I suggest you make the number of digits an instance field/property.

--
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
Thanks Ollie, that was a briliant soolution, especily since its so
simple. :)

BTW: Jon, This isn't actul in use code, its just something I wrote for
test porposes to explore what c# could do.

thanks,

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
13740
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would like to have each object of my class have unique values of these variables, because the values may be different depending on some other factor in the object. Something like this:
2
1877
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
115
7642
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical transform function. When compiling under gcc on my big-endian PowerPC (Mac OS X), declaring this array as "static" DECREASES the transform throughput by around 5%. However, declaring it as "static" on gcc/Linux/Intel INCREASES the throughput by...
4
42045
by: Wayne | last post by:
Hi, I'm new to .NET and have a question about the use of static variables vs. session variables in a web form in C#. Instead of using a session variable to hold a string to persist during datagrid paging. Can I use a static variable? Also, if I use a static variable is that variable shared by everyone that brings up the aspx page? Ex, if 4 people are viewing the aspx page is an instance of the static variable created for each of the...
4
10763
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...
25
5180
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I wanna use global.asax) --- Would you declare your static var as --- public static int x ;
8
6846
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx code-behind and in business logic (C# classes used by the aspx) lose their value. I cannot reproduce this on our development server, so I cannot understand what the cause of all this is. We are using asp.net 1.1 with IIS6 on win2003.
5
6780
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that (1) holds for unmanaged C++ code, but not for managed code. I have class library with both managed and unmanaged static variables that are not referenced by any part of the program. All the
9
8656
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
16
8624
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions - which is Very Bad (tm) for reasons I understand! However, does this rule apply only to global static variables, or does it apply to procedure-level static variables.
0
9590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
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
10051
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...
0
9866
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...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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
5310
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
3968
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
2815
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.