Search code examples
c#state-machine

State machine, what to use instead of enum to store multiple states eg ( 0% to 100% for a vehicle's drive base height)


enum RideHeight_State 
    {
        rideHeight = 100
    }

I'm trying to use rideHeight variable as if it is an int by calling:

RideHeight_State.rideHeight -= 10; // Decrement rideHeight State by 10 

inside a private void function later on...

Any thoughts on where I'm going wrong?

Thank You


Solution

  • It sounds like you may need to do this:

    enum RideHeight_State
    {
        rideHeight,
    }
    
    private Dictionary<RideHeight_State, int> _state = new Dictionary<RideHeight_State, int>()
    {
        { RideHeight_State.rideHeight, 100 },
    };
    

    Then this code might work more like you expect:

    _state[RideHeight_State.rideHeight] -= 10;
    
    Console.WriteLine(_state[RideHeight_State.rideHeight]);
    

    That outputs 90.

    Your naming convention for your enum is not very clear though. I wasn't sure what to suggest there.