本文介绍了VB.NET 将 Select Case 语句堆叠在一起,就像在 Switch C#/Java 中一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

你的理解是正确的.VB 不会失败".

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

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

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

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

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.

这篇关于VB.NET 将 Select Case 语句堆叠在一起,就像在 Switch C#/Java 中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:34