473,326 Members | 2,588 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,326 software developers and data experts.

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.WriteLine("a = {0}, b = {1}", a.desc(), b.desc());
a.grow();
b.grow();
Console.WriteLine("a = {0}, b = {1}", a.desc(), b.desc());
a.shrink();
b.shrink();
Console.WriteLine("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 2281
Bump,

I know this works, but is there something cleaner?

public void grow()
{
int v = (int)this.GetType().GetField("PostCodeDigits").Get Value(this);
v++;
this.GetType().GetField("PostCodeDigits").SetValue (this, v);
}

Nov 17 '05 #2
you can't specify that a field can be marked as virtual\override, 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.WriteLine("a = {0}, b = {1}", a.Desc(), b.Desc());
a.Grow();
b.Grow();
Console.WriteLine("a = {0}, b = {1}", a.Desc(), b.Desc());
a.Shrink();
b.Shrink();
Console.WriteLine("a = {0}, b = {1}", a.Desc(), b.Desc());
Console.ReadLine();
}
}

public class Letter
{
private static int _postCodeDigits = 4;
public virtual int PostCodeDigits
{
get
{
return _postCodeDigits;
}
set
{
_postCodeDigits = value;
Console.WriteLine("Setting post code digit length for
letter - " + value);
}
}
public void Grow()
{
this.PostCodeDigits++;
}
public void Shrink()
{
this.PostCodeDigits--;
}
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.WriteLine("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.googlegr oups.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.WriteLine("a = {0}, b = {1}", a.desc(), b.desc());
a.grow();
b.grow();
Console.WriteLine("a = {0}, b = {1}", a.desc(), b.desc());
a.shrink();
b.shrink();
Console.WriteLine("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.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
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
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...
2
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
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...
4
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...
4
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...
25
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...
8
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...
5
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...
9
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...
16
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 -...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.