VB.NET Stacking Select Case Statements together like in Switch C#/Java

↘锁芯ラ 提交于 2019-12-21 07:08:34

问题


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.`


回答1:


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.



来源:https://stackoverflow.com/questions/23795886/vb-net-stacking-select-case-statements-together-like-in-switch-c-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!