-4

I am using a switch and get a warning that code is unreachable.

Switch warning

If control goes to case 2 or 3, something will get execute then what is the issue?

  • 5
    The control flow will never reach the `break` statement, because of the `gotos` – Matthew Watson Apr 11 '18 at 09:04
  • Somethings from either if or else will be executed, but as you are using goto in both the block, code will never reach to break statement, and that's the reason it's showing unreachable code message. – Dishant Apr 11 '18 at 09:05
  • As it says, that break operation is unreachable. So you can remove that line. – Danielle Summers Apr 11 '18 at 09:05
  • 2
    Do you really need a switch statement here at all? that maze of goto's makes it look like you just need to refine the logic into if statements – Sayse Apr 11 '18 at 09:05
  • The issue is that no matter what happen, that line is never going to be hit, because you are altering the execution flow – MrVoid Apr 11 '18 at 09:05

2 Answers2

2

While executing case "2" and case "3", your function pointer will never reach break statement because of goto statements in if..else conditions.

Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
2

It is unreachable, the if statement above it will always redirect with the gotos.

You could just refactor the logic into a simple if statement..

var cemValIsOne = hdnCEMStatus.Value == "1";
var cemIDIsZeroOrEmpty = hdmCEMID.Value == "" || hdmCEMID.Value == "0";
if(cemValIsOne || (!cemValIsOne && !cemIDIsZeroOrEmpty))
{
     // case 1 code...
}
else
{
    // case 0 true if statement code
}
Sayse
  • 42,633
  • 14
  • 77
  • 146