c# - If Condition inside switch case -


this question has answer here:

i trying convert if statement switch cases (for readability)

1) i've read switch statements aweful in general - true? https://stackoverflow.com/questions/6097513/switch-statement-inside-a-switch-statement-c

2) statement goes 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 1 case label ('case 1:') another

this original if statement:

if ((show == display.all) || (expected.expense == true && show == display.expense) || (expected.expense == false && show == display.nonexpense)) {     //code } 

the compiler not understand mean here.

switch (show) {     case display.expense:         if (expected.expense != true)             break;         // missing break here     case display.nonexpense: 

the compiler not connect dots , understand break; statement inside if statement linked switch statement. instead try link loop, since break; statements on own can used loops, break out of it.

that means case block missing break statement complete it, , compiler complains.

instead of trying wring necessary code out of switch statement, instead break original if statement.

this yours:

if ((show == display.all) || (expected.expense == true && show == display.expense) || (expected.expense == false && show == display.nonexpense)) {     //code } 

this how write it:

bool dodisplayexpected =        (show == display.all)     || (show == display.expense    && expected.expense)     || (show == display.nonexpense && !expected.expense); if (dodisplayexpected) {     // code } 

you don't have pack on 1 line.

also, try name properties they're easier read, rename expense property isexpense above code read this:

bool dodisplayexpected =        (show == display.all)     || (show == display.expense    && expected.isexpense)     || (show == display.nonexpense && !expected.isexpense); if (dodisplayexpected) {     // code } 

then, ideally, refactor out sub-expressions 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 can put expression if-statement:

if (showall() || showexpense(expected) || shownonexpense(expected)) {     // code } 

this should easier read, , change later on.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -