Access the static property of the child in the parent method

Let's say I have the following code:

class Parent { static string MyField = "ParentField"; public virtual string DoSomething() { return MyField; } } class Child : Parent { static new string MyField = "ChildField"; } 

Now I want to be able to do one of the following:

 Console.WriteLine(Parent.MyField); Console.WriteLine(Child.MyField); 

They work as expected, but I would also like to do this:

 Child c = new Child(); Console.WriteLine(c.DoSomething()); 

Since DoSomething () is not defined for the Child class, the parent MyField is returned, but I want the Child MyField to be.

So my question is: is there a way to do this?

NOTE. Overriding a method in the Child class is an option, but since I will have many child classes inheriting from the parent, and the method should do the same in all of them, changing something in this method will lead to a lot of trouble.

+3
source share
9 answers

If you require a design that is not supported by the language, in which case static virtual members, this is a smell that indicates that you may have the wrong design.

Instead of showing us a solution, show us the problem you are trying to solve with this solution. I assume that there is a completely different design that eliminates the need for this oddity.

In other words: the answers you received are not the ones you need, because the question you asked is not the question you should ask. I do not think that the proposed design does not provide advantages that another, more ordinary design does not.

+7
source

You must deal with this in Child too:

 class Child { static new string MyField = "ChildField"; public override string DoSomething() { return MyField; } } 

However, using a single virtual property is likely to be cleaner since you don’t need a “new” keyword to hide the Parent member field.

+1
source

I don’t understand why you think that you need both “static” and “non-static” environments to call different properties / functions that return the same thing. If you just do this:

 class Parent { public virtual string DoSomething() { return "ParentField"; } } class Child { public override string DoSomething() { return "ChildField"; } } 

Then this will work the way you want:

 Child c = new Child(); Console.WriteLine(c.DoSomething()); 

And instead:

 Console.WriteLine(Parent.MyField); Console.WriteLine(Child.MyField); 

you just write this:

 Console.WriteLine(new Parent().DoSomething()); Console.WriteLine(new Child().DoSomething()); 

Is there any other restriction on the problem that makes this unacceptable? Is the creation of any new objects of these classes excessively expensive, for example?

+1
source

Use a static property instead of a static variable. You can then override the property in the child class instead of creating a "new" field.

0
source

Yes, override DoSomething:

 class Child { static new string MyField = "ChildField"; public virtual string DoSomething() { return MyField; } } 
0
source

There is no inheritance of magic for anything static, but you can do it

 protected virtual string { get { return "your value"; } } 

and use the property inside your method

0
source

In one answer, you seem to indicate that the static field is constant. If so, then this should work for you.

 class Parent { protected virtual string MyField() { return "ParentField"; } public virtual string DoSomething() { return MyField(); } } class Child : Parent { protected override string MyField() { return "ChildField"; } } 
0
source

The only way is to override the DoSomething method, otherwise it is not possible. The DoSomething in Parent method essentially means:

 public virtual string DoSomething() { return Parent.MyField; } 

When you are a “ new ” property, it applies only to this type — in this case, the Child class. If the resource is accessed through the "Parent", it will always return the original property.

0
source

First, let me say that you really just need to make MyField virtual and accept that you need to create an instance to get it. However, another way to "solve" the problem would be:

 class Parent { public static string MyField = "ParentField"; protected virtual MyLocalField = MyField; public virtual string DoSomething() { return MyLocalField; } } class Child : Parent { public static new string MyField = "ChildField"; protected override MyLocalField = MyField; } 
0
source

Source: https://habr.com/ru/post/888245/


All Articles