0

Say I have this in VB.NET:

Dim executeB As Boolean
Select Case myVariable
  Case "a"
    'some code
  Case "b"
     'some code
  Case Else
End Select

If myVariable is "a", control will go into the first case statement. Now let's say if myVariable = "a", but inside a's case block, I find that executeB is true, is it then possible to jump to the second case?

millimoose
  • 39,073
  • 9
  • 82
  • 134
Prabhu
  • 12,995
  • 33
  • 127
  • 210
  • 1
    Why do you have c# in your tag list, if you're looking for VB code? – Jon Skeet Oct 18 '11 at 17:51
  • possible duplicate of [VB.NET Switch Statement GoTo Case](http://stackoverflow.com/questions/820104/vb-net-switch-statement-goto-case) – Jon Skeet Oct 18 '11 at 17:51
  • @Jon...I just chose to cite the example in VB.NET, but I was interested in knowing the answer for both C# and VB.NET (that's why I titled the question "Switch/Select". – Prabhu Oct 18 '11 at 18:07
  • 1
    It's worth being clear about this sort of thing in the question, in future. – Jon Skeet Oct 18 '11 at 18:14

5 Answers5

5

Yes, encapsulate the code within the "b" case into a function/procedure and call it from case "a"

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • That's what I'd normally do, but I wanted to see if the framework allows you to go from one case to another. – Prabhu Oct 18 '11 at 18:05
  • @Prabhu I understand. The other alternative is the GOTO which most programming languages support, including VB.NET. I don't really like the idea, that's why I didn't propose it. – Icarus Oct 18 '11 at 18:38
1

If you need to jump from case A to case B, then a switch probably isn't the correct thing to be using. The switch structure is based on the idea that each path is mutually exclusive.

But to answer the actual question, no I'm pretty sure you can't jump into a case.

Toomai
  • 3,974
  • 1
  • 20
  • 22
  • Thanks, that's really what I wanted to know. In that case, I probably have to refactor as @lcarus suggested. – Prabhu Oct 18 '11 at 18:10
1

You could use the GoTo statement. That said, you shouldn't, it's almost never the right thing to do and I can't remember an instance where it actually would make code clearer as opposed to break, continue and return, its more structured cousins. (Or as opposed to breaking up the code so neither is necessary.) Wrap the switch case bodies in functions and just call those.

millimoose
  • 39,073
  • 9
  • 82
  • 134
0

In that case, I you may want to make whatever "executeB" is into a private function and just call that.

Reddog
  • 15,219
  • 3
  • 51
  • 63
0

Just a note, in general what you're trying to do is bad, it's called waterfalling. .Net tries to prevent you from doing it, because it introduces hard to troubleshoot bugs into your code. If you trully have compound conditions you'd want to use a normal if/then/else.

  • I wouldn't normally do this, but I'm working on someone else's code and need to find a way to modify something without refactoring too much. – Prabhu Oct 18 '11 at 18:04