Best C# Way to Have Unique Static Data on Subclasses?

Since you can’t override static ‘class’ variables in C# , (and it’s a pain to access static vars from an instance anyway) is it better use a hardcoded method, a hardcoded property, or some other way?

What I would like to do but can’t

class MoraleState {
 public static virtual bool canFire = false;
...
class MoraleOK : MoraleState {
 public static override bool canFire = true;

The two ways I’ve found to do it. Of the two, is there a preferred way?

//using property
class MoraleState {
 public virtual bool canFire { get { return false; } }
...
class MoraleOK : MoraleState {
 public override bool canFire {
   get { return true; }
 }
//using method
class MoraleState {
 public virtual bool CanFire() { return false;}
...
class MoraleOK : MoraleState {
 public override bool CanFire() {
    return true; 
 }
1 Like

Is there a reason you want them static? I have a feeling they don’t need to be, in which case would this work instead?

public class MoraleState
{

	private bool m_CanFire = false;
	public bool CanFire
	{
		get
		{
			return this.m_CanFire;
		}
		protected set
		{
			this.m_CanFire = value;
		}
	}
}

public class MoraleOK : MoraleState
{
	public MoraleOK()
	{
		this.CanFire = true;
	}
}

Nice one FizixMan.

About static variables:
The meaning of a static variable is that it will be same for all instances of the class, so that means that they can’t be inherited and they exist only in the class where they were declared.

About properties:
Practically properties and methods are very similar, most freshmen wonder why this happens, why not instead strictly use methods and avoid properties. The importance of properties is that they are better suited for exposing private/protected variables (setting or getting values). For example, in Java programming language it’s standard to create 2 public methods for getting and setting the variable.

So the winner is elegance of code…

Instead of doing this:
float gold = player.GetInventory().GetGoldBag().GetAmount();

You can use properties and do this:
float gold = player.Inventory.GoldBag.Amount;

Thanks for answering. I wanted them static since all instances of a subclass return the same value for CanFire. Statics are a nice way to handle this, as I’m sure you know.

My CanFire example is pretty simple. A more complex one is where I want all instances of a subclass to return the same value, but that value may change from run to run as it is read from an xml file. How does this look?

public class PostureState {
  public abstract float speed { get; }

public class PostureWalking : PostureState { 
  private static float _speed  = ReadFromConfig("PostureWalking.speed"); //or done in static constructor
  public override float speed { 
    get {
     return _speed;
    }
  }

Also, is m_ a standard prefix for private variables?

Statics are not a good way of handling it the way you’re talking about. If you’re talking about subclasses and inheritance, then you should not use statics. Why? Because you’re not actually using inheritance and it’s just plain confusing.

The only other takeaway I can figure is maybe you’re using only a single instance of these classes? (why make more than one PostureWalking?) Perhaps then you should look into the singleton pattern.

EDIT: I reread your code there. That’s a fine pattern (you’re missing the inheritance call though). Maybe just make your _speed variable “readonly”.

Thanks FizixMan. It’s for a state machine. I may be able to go with singletons of the States and have multiple instances of the StateMachine which contains them. It just means I have to store all instance varying data on the StateMachine rather than in each State. Not sure yet which is the way to go, a little experimentation is in order.