IF CONDITION INSIDE SWITCH CASE

jayprakash sharma

Total Post:117

Points:821
Posted by  jayprakash sharma
C# 
C#
 1462  View(s)
Ratings:
Rate this:
Hi Everyone!

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?

http://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
}

Advance Thanks
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: If Condition inside switch case

    Hi Jayprakash!

    used switch condition within if statement as below

    switch (Show)
    {
        case Display.Expense:
             if (expected.EXPENSE != true)
                 // do what you want
                 break;
             else 
                 // do what you want
                 break;
        case Display.NonExpense:
             if (expected.EXPENSE == true)
                 // do what you want
                 break;
             else 
                 // do what you want
                 break;
        case Display.All:
            //Code
            break;
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!