1

i have a class with a static field and a static function, e.g. like this

public class A {
    protected static string[] _eventField = new[] { "SomeValue" };

    public static TOut DoSomethingThatDependsOnEventField(TIn input){
        //output depends on input and the static _eventField
    }

    public class Nested1: A {
        protected new static string[] _eventField = new[] { "SomethingDifferent" };
    }

    public class Nested2 : A {
        protected new static string[] _eventField = new[] { "SomethingElse" };
    }
}

The output and input types of that static method are of no importance here, the only relevant thing is that the output - despite relying on the input, of course - depends on the content of the static field. The implementation of the method doesn't change at all in the derived classes, and all I want is to change that very static field. But whenever I do a call like

var res1 = A.Nested1.DoSomethingThatDependsOnEventField(...);

or var res2 = A.Nested2.DoSomethingThatDependsOnEventField(...);

the incorrect static field from the base class A is referenced from within the method. That is, the intended "hiding" / "redefining" of the static field via protected new static string[] _eventField = ... doesn't work - Why is that so?

MrCC
  • 714
  • 8
  • 20
  • Why use nested static classes? – Dustin Kingen Jun 19 '13 at 13:19
  • @Romoku Why not? It's just for convenience, do you expect a different behavior when the classes are not nested? (it doesn't behave any different afaik). – MrCC Jun 20 '13 at 05:39
  • Could someone of the "duplicate" taggers please comment on *why* they think that this is a duplicate of question [248263](http://stackoverflow.com/questions/248263/why-cant-i-declare-c-sharp-methods-virtual-and-static)? I'm not asking about "virtualizing" static methods, all I ask is about changing a piece of data this static method is operating on. – MrCC Jun 20 '13 at 06:04
  • In fact, after tinkering with it for a while, I think I found a "workaround" for the weird fact that you supposedly cannot redefine static fields. I would post some code but I think the "duplication" tag prevents me from "adding an answer to my own question". Basically, it boils down to the fact the the field redefinition via the "new" keyword seems not to "hide" the field from class A. What you can do however, is redefine the static method via "new" in a dervied class and set "A._eventField = new[] { "SomethingDifferent" }" before calling "return A.DoSomethingThatDependsOnEventField(..)" – MrCC Jun 20 '13 at 06:44

1 Answers1

0

You cant have an object associated with Static field/member/function , so static field/member/function cannot come into picture in Inheritance also.

srsyogesh
  • 609
  • 5
  • 17
  • A static field is just a location in memory just like a non-static field is, with the only difference that it's not per-object. Why do you believe that you actually should not be able to change the content of this memory? – MrCC Jun 20 '13 at 06:17
  • @ sorry that if i havnt explained properly , but i never mentioned you cant change a value of static field . The point is it cant participate in inheritance. – srsyogesh Jun 20 '13 at 14:43
  • You might be surprised that for methods static inheritance seems to work; see my other comment for details. – MrCC Jun 24 '13 at 07:04