23

Seems If I stack the Cases together they don't work as one. Since VB.NET Cases don't require the use of Exit Select / Return it seems to automatically put that every time a new Case is detected under it?

Dim Test as Integer = 12

Select Case Test
  Case 11
  Case 12
  Case 13
    MsgBox.Show("Could be 11 or 12 or 13?")
End Select

It doesn't seem to work only 13 works..

Gotta always remember this rule that you can't stack Cases like this from now on
It's not easy to remember it when porting applications.`

3
  • The dotnet perls site drived me in the same error...
    – Zac
    Jan 19, 2016 at 11:48
  • Haha yes someone must report to that site that they got it wrong
    – SSpoke
    Jan 19, 2016 at 21:40
  • 1
    It only "doesn't work" if you think of the way C does it as "correct" and anything not like C as "wrong". You might as well say that VB doesn't work because putting braces to mark the beginning and end of a loop gave you an error message. :-)
    – Jay
    Aug 30, 2018 at 19:31

1 Answer 1

35

Your understanding is correct. VB will not "fall through".

Specify a single Case and separate each expression with a comma:

Select Case Test
    Case 11, 12, 13
        MsgBox.Show("Could be 11 or 12 or 13?")
End Select

Alternatively, you could use a range with the To keyword to accomplish the same thing:

Select Case Test
    Case 11 To 13
        MsgBox.Show("Could be 11 or 12 or 13?")
End Select

For more information, see the documentation.

3
  • 1
    Thanks They should make it detect the pattern above if they are all empty the same thing but then again you'll lose the ability to use it as ignore filter.
    – SSpoke
    May 22, 2014 at 0:23
  • That's up for argument. I might agree because I'm used to the pattern, but it might be confusing for many VB people. Consider the case where you had a single statement after the case which, during debugging, you comment out. Because VB doesn't require a "break" it would be really hard to explain the new behavior - why it's started to fall through. It's not explicit behavior, nor is it clear looking at the code.
    – lc.
    May 22, 2014 at 0:28
  • 1
    Ya now that I think about it they probably shouldn't change it to now tbh since it's part of how BASIC operated from the beginning for ages plus it seems to make up for that with a better feature that the switch doesn't have just seen the < > comparisons in related threads pretty powerful stuff. Gotta always remember this rule that you can't stack them from now on it's not easy to remember it when porting applications.
    – SSpoke
    May 22, 2014 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.