Can I have a base class where each derived class has its own copy of a static property?

You will need to redefine and hide the field and method in all derived types.

You will need to redefine and hide the field and method in all derived types. Example: class DerivedA : Base { public new static int x; public new int myMethod() { x += 5; return x; } } Note: don't do it this way. Fix your design.

Edit: Actually, I have a similar construct. I solve it with an abstract (if you need a default value, use virtual) property which then gets used from the base class: public abstract class Base { public abstract string Name { get; } public void Refresh() { //do something with Name } } public class DerivedA { public override string Name { get { return "Overview"; } } } You should be able to adjust that for your use case. You can of course make the property protected if only deriving classes should be able to see it.

The method would be the same for each of my derived classes. – ntsue Feb 20 at 21:37 1 Please edit your question to display what you want to achieve. We will be able to help you more that way.

– Femaref Feb 20 at 21:39 @Femaref, edited – ntsue Feb 20 at 21:52 So, I took the code you gave and modified it by adding a private static variable to DerivedA... and I use the overridden property to get and set that static field... Is that ok? Or is that not a good way to do it? It seems to do what I want.. thanks!

– ntsue Feb 21 at 14:41 Well, the point is to not use any static field at all - you should use just the property on its own. The point is that you don't have to add a static field everytime you derive but simply override the property and handle your case there. – Femaref Feb 21 at 14:44.

Well, yes, you can, but it revolves around a bit of a trick with generics. It is much better if you fix your design so that you don't need that static field, or at least not per descendant, but here goes: class Base where TDescendant : Base { public static int x; public int myMethod() { x += 5; return x; } } class DerivedA : Base { } class DerivedB : Base { } This relies on the fact that a generic type with static fields will get a separate copy of those static fields for each type you invoke it with. However, if you intend to descend from DerivedA or DerivedB, it gets tricky, so I wouldn't recommend going down this lane.

I usually implement subclass specific stuff as an abstract get property public class Base { // You must pick one option below // if you have a default value in the base class public virtual int x { get { return 7; /* magic default value */} } // if you don't have a default value // if you choose this alternative you must also make the Base class abstract public abstract int x { get; } } public class DerivedA : Base { public override int x { get { return 5; } } } public class DerivedB : Base { public override int x { get { return 10; } } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions