VB.NET 将 Select Case 语句堆叠在一起,就像在 Switch C#/Java 中一样

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23795886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:35:06  来源:igfitidea点击:

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

vb.netswitch-statementcase-statement

提问by SSpoke

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?

似乎如果我将案例堆叠在一起,它们就不会合二为一。由于 VB.NET Cases 不需要使用Exit Select/ Return 似乎每次在它下检测到新 Case 时都会自动放置?

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

似乎只有 13 个作品不起作用..

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

Gotta always remember this rule that you can't stack Cases like this from now on
移植应用程序时不容易记住它。`

回答by lc.

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

你的理解是正确的。VB 不会“失败”。

Specify a single Caseand separate each expression with a comma:

指定单个Case表达式并用逗号分隔每个表达式:

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 Tokeyword to accomplish the same thing:

或者,您可以使用带有To关键字的范围来完成相同的事情:

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

For more information, see the documentation.

有关更多信息,请参阅文档