Inheritance on static variables?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.

.



Relevant Pages

  • Re: An absence of IntelliSense in this situation
    ... > event source and only need to know if and when it fires an event then ... > Public Class CBaseEventSourceContainer ... > End Sub ... >>> As you say you still have access to the base class event source ...
    (microsoft.public.dotnet.languages.vb)
  • Re: An absence of IntelliSense in this situation
    ... event source and only need to know if and when it fires an event then you ... Public Class CBaseEventSourceContainer ... Private Sub MyEventHandler ... >> As you say you still have access to the base class event source so ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Derived class Interface method needs to call Base Class Interface Method
    ... public interface ITest ... public class A: ITest ... public void Func() ... method defined in a base class - the base class interface method was not ...
    (microsoft.public.dotnet.languages.csharp)
  • C# questions about basic constructs
    ... Why C# differs as to base class visibility in regard to Derived ... Can we member hiding by re-implementing the method in the derived ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: OO question on overriding virtual methods
    ... > I'm actually developing architectural frameworks for application ... > flexibility they introduce (flexibility really needed in framework ... called directly on the base class, then why don't you understand how ... > public class B:A ...
    (microsoft.public.dotnet.languages.csharp)