problem with enum and switch case c#

i declared enum ==> Direction in a script called ==> ( Settings )
then i try using it in switch case
but it is give me that error

Expression denotes a type', where a variable’, value' or method group’ was expected

switch ( Settings.Direction)
                {
                case Direction.Right:
                  
                    break;
                case Direction.Left:
                
                    break;
                case Direction.Up:
                    
                    break;
                case Direction.Down:
              
                    break;
                }

You’re supposed to pass a variable inside the switch statement to evaluate. Currently you’re just passing the type. Think of a switch statement as just a nice looking alternative to a load of if…else if statements if you’re struggling to understand it.

Create a Settings.Direction variable called settingsDirection or something similar and switch ( settingsDirection )

You could also use a Vector2 direction that contains 0,1 when up, 1, 0 right, -1, 0 left, and 0,-1 down, it may be more useful later on than using an switch/enum for this

These guys here are correct. Visualized:

var settings = Direction.Right;

switch (settings)
            {
                case Direction.Right:
                    //In this case, this code block will be executed.
                    break;
                case Direction.Left:
             
                    break;
                case Direction.Up:
                 
                    break;
                case Direction.Down:
           
                    break;
                }
}
1 Like