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