Inheritance on static variables?

S

shooter

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.
 
S

shooter

Bump,

I know this works, but is there something cleaner?

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

Ollie Riches

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
 
J

Jon Skeet [C# MVP]

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.
 
S

shooter

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top