I have a question similar to this one: Can I have a base class where each derived class has its own copy of a static property?, but the answer there didn't seem very conclusive to me.
Basically, I have a class that has a constant property, and I want to derive a class that has all the same methods but uses a different value for the variable. For a simple analogy, imagine that I had a class that looked, in part, like this:
public class Triangle
{
public const NumSides = 3;
int sideLength;
public int Perimeter()
{
return NumSides * sideLength;
}
}
and now I wanted to make a Square class that was exactly like the Triangle class, except NumSides was now 4.
In other words, I want a class that now looks, in its entirety, something like:
public class Square : Triangle
{
public const NumSides = 4;
}
but that still has access to all the methods of the Triangle class. (I can handle writing a new constructor if necessary, but I don't want to have to copy over the dozen or so methods from the original class. They're all supposed to behave exactly the same, but with NumSides now having a value of 4.)
I can't figure out how to do this, either with NumSides being a const or as a static property with only a get function. If I try to override the constant, I get "The modifier 'override' is not valid for this item." When I try to override the static property version, I get the message equivalent of "A static member Triangle.NumSides cannot be marked as override, virtual, or abstract."
And, yes, I've tried creating a Shape class, putting all the functionality in there, and then having both Triangle and Square inherit from that class. I run into the same problem with NumSides though. How can I change the effective value of a constant or static variable/property when deriving a new class?