Override static derived variable




Hello.

I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to define some derived classes wich inherit the function and override the variable, but it doesn't work.

Testcase:

using System;

namespace Test
{

  abstract public class X {
    protected static string var = "x";

    public static void print_my_var(){
      Console.WriteLine("my var is " + var);
    }
  }

  public class A : X {
    protected static new string var = "a";
  }

  public class B : X
  {
    protected static new string var = "b";
  }

  class ClassTest {

    [STAThread]
    static void Main(string[] args)
    {
      X.print_my_var();
      A.print_my_var();
      B.print_my_var();
    }
  }
}


result: my var is x my var is x my var is x

expected results:
my var is x
my var is a
my var is b

How can obtain the expected results using static methods and static variables and without creating the static method A or B?

Thanks in advance.

	Andrew

--
.



Relevant Pages

  • Re: Declaring final variables
    ... i've read the manpage on creating a static variable..but i still dont ... if $var is static, then $string should print out "This is overwrite my ... This has nothing whatsoever to do with static variables. ...
    (comp.lang.perl.misc)
  • Re: Declaring variables outside subroutines
    ... > can't help finding it disturbing that in the code below, $var is in scope ... It's standard procedure for creating static variables in C. ...
    (comp.lang.perl.misc)
  • Re: Class name as a variable
    ... def abc end ... is there any better (or nicer) way to doing this? ... var = B ... Ye, they should be static methods, else it will be wrong. ...
    (comp.lang.ruby)