spike.y
spike.y

Reputation: 399

Why isn't my enum working in the switch statement?

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?

Upvotes: 0

Views: 2464

Answers (1)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

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)

Upvotes: 5

Related Questions