Home > DeveloperSection > Forums > If Condition inside switch case
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    April-06-2013 2:01 AM

 C# C# 
Ratings:


 1 Reply(s)
 1114  View(s)
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


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    April-06-2013 9:05 AM

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;
}

Don't want to miss updates? Please click the below button!

Follow MindStick