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.