0

I remember using enums in a switch statement in the past, and according to C# how to use enum with switch I am doing it the right way. But I've just tried to do it again and I received the following error:

'ApplicationMode' is a 'type' but is used like a 'variable'.

Here's the code I am using:

public static enum ApplicationMode
{
    Edit,
    Upload,
    Sync,
    None
}

private void edit_Click(object sender, EventArgs e)
{
    switch(ApplicationMode) // This is where I see the error.
    {
        case ApplicationMode.Edit:
            break;
        ...
    }
}

What have I done wrong?

Community
  • 1
  • 1
spike.y
  • 389
  • 5
  • 17
  • 3
    Error message told the exact reason. – qxg Apr 26 '14 at 02:24
  • 2
    The switch statement expects you to supply a variable of type `ApplicationMode` rather than the type itself. That's why you're getting a compile error. You don't show a variable of type `ApplicationMode` in your program anywhere. Is there one set? More to the point, what are you trying to do? The user clicked the `Edit` button. Do you want to edit now, or do you want to set the mode to `Edit`? – Jim Mischel Apr 26 '14 at 02:25

1 Answers1

5

Problem 1: enums are static by default so don't declare them as static.

Solution 1: you need to remove the static keyword in enum declaration

public enum ApplicationMode
{
    Edit,
    Upload,
    Sync,
    None
}

Problem 2: in switch case you need to provide the enum ApplicationMode variable which contains any valid enum value [Edit,Upload,Sync,None], but you are trying to provide the enum type ApplicationMode itself.

Solution 2: provide the enum ApplicationMode variable which contains any valid value.

Try This:

ApplicationMode appMode = ApplicationMode.Upload; //assign any value
switch(appMode)
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • Thank you @Sudhakar, you just reminded me of how I used to do this. Your answer was very helpful. – spike.y Apr 26 '14 at 03:05