If Condition inside switch case

c#, if-statement, switch-statement

Solution

The compiler will not understand what you mean here.

switch (Show)
{
    case Display.Expense:
        if (expected.EXPENSE != true)
            break;
        // missing break here
    case Display.NonExpense:

The compiler will not connect the dots and understand that the `break;` statement inside your `if` statement is linked to the `switch` statement. Instead it will try to link it to a loop, since `break;` statements on their own can only be used with loops, to break out of it.

That means that your `case` block is missing its `break` statement to complete it, and thus the compiler complains.

Instead of trying to wring the necessary code out of a `switch` statement, I would instead break up your original `if` statement.

This is yours:

if ((Show == Display.All) || (expected.EXPENSE == true && Show == Display.Expense) || (expected.EXPENSE == false && Show == Display.NonExpense))
{
    //Code
}

This is how I would write it:

bool doDisplayExpected =
       (Show == Display.All)
    || (Show == Display.Expense    && expected.EXPENSE)
    || (Show == Display.NonExpense && !expected.EXPENSE);
if (doDisplayExpected)
{
    // code
}

You don't have to pack everything on one line.

Also, I would try to name properties so that they're easier to read, I would rename the `EXPENSE` property to `IsExpense` so that the above code would read like this:

bool doDisplayExpected =
       (Show == Display.All)
    || (Show == Display.Expense    && expected.IsExpense)
    || (Show == Display.NonExpense && !expected.IsExpense);
if (doDisplayExpected)
{
    // code
}

Then, ideally, I would refactor out the sub-expressions to methods:

bool doDisplayExpected =
       ShowAll()
    || ShowExpense(expected)
    || ShowNonExpense(expected);
if (doDisplayExpected)
{
    // code
}

public bool ShowAll()
{
    return Show == Display.All;
}

public bool ShowExpense(Expected expected)
{
    return Show == Display.Expense && expected.EXPENSE;
}

public bool ShowNonExpense(Expected expected)
{
    return Show == Display.NonExpense && !expected.EXPENSE;
}

Then you can put the expression back into the if-statement:

if (ShowAll() || ShowExpense(expected) || ShowNonExpense(expected))
{
    // code
}

This should be easier to read, and change later on.

Problem

I am trying to convert an if statement to switch cases (for readability) 1) I've read switch statements are aweful in general - Is that true? https://stackoverflow.com/questions/6097513/switch-statement-inside-a-switch-statement-c 2) The statement goes like this: ``` switch (Show) { case Display.Expense: if (expected.EXPENSE != true) break; case Display.NonExpense: if (expected.EXPENSE == true) break; case Display.All: //Code break; } ``` Error is: Control cannot fall through from one case label ('case 1:') to another This is the original if statement: ``` if ((Show == Display.All) || (expected.EXPENSE == true && Show == Display.Expense) || (expected.EXPENSE == false && Show == Display.NonExpense)) { //Code } ```

Original source

Related problems