什么是带有 case OR-ing 的 VB.NET select case 语句逻辑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/555197/
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-09 14:03:59  来源:igfitidea点击:

What is the VB.NET select case statement logic with case OR-ing?

vb.netsyntaxswitch-statement

提问by Llyle

I'm using an Orstatement in my case expression.

我在 case 表达式中使用了Or语句。

Even though I have a value within this range, it didn't find a match. Why not?

即使我在这个范围内有一个值,它也没有找到匹配项。为什么不?

Example Code:

示例代码

Select Case 2
    Case 0
        ' Some logic

    Case 1
        ' Some other logic

    Case 2 Or 3
        Console.WriteLine("hit")

 End Select

With the above I would assume that hitwould be printed, but that's not the case.

有了上面的内容,我假设hit会被打印出来,但事实并非如此。

回答by JaredPar

Use the comma operator to delimit case statements

使用逗号运算符分隔 case 语句

Select Case 2
    Case 0,1,2,3
        Console.WriteLine("hit")
 End Select

回答by John T

As Jaredsaid, you need to use the comma operator to delimit case statements.

正如Jared所说,您需要使用逗号运算符来分隔 case 语句。

The Oryou were doing is a bitwise OR, resulting in it being "3". Amusingly, "2 AND 3" would probably have worked for your specific case.

Or你在做一个按位OR,导致它成为“3”。有趣的是,“2 AND 3”可能适用于您的特定情况。

回答by Jason Punyon

JaredPar has it right but you can also use the To construct

JaredPar 说得对,但您也可以使用 To 构造

Select Case 2
    Case 0,1
    Case 2 To 3
        Console.WriteLine("Hit")
End Select

This would be 0 or 1 do nothing, 2 or 3 print Hit...The To construct is a range...

这将是 0 或 1 什么都不做,2 或 3 打印 Hit...To 构造是一个范围...

Here's the MSDN

这是 MSDN

回答by user67143

Edit:It appears I was wrong in assuming that VB.NET doesn't allow Case ORing. I was thinking in C# and IL and it appears I was wrong.

编辑:看来我假设 VB.NET 不允许 Case ORing 是错误的。我在用 C# 和 IL 思考,看来我错了。

However, as someone pointed out, the reason your code did not work was because Case 2 Or 3 was evaluating 2 Or 3 as a bitwise or and hence evaluating to Case 3.

但是,正如有人指出的那样,您的代码不起作用的原因是案例 2 或 3 将 2 或 3 评估为按位或,因此评估为案例 3。

For clarification:

为了澄清:


       2 binary = 0000 0010
       3 binary = 0000 0011
  2 Or 3 binary = 0000 0011 (= 3)

  Select Case 2
     Case 0            '--> no match

     Case 1            '--> no match

     Case 2 Or 3       '(equivalent to Case 3  --> no match)
   End Select

However, I feel that I should point out that for the sake of performance, one should not use such constructs. When the compiler encounters Select statements (switch in C#) it will try to compile them using lookup tables and the switchMSIL instruction but in the case where you have something like Case 1,2,11,55the compiler will not be able to convert that to a lookup table and it will have to use a series of compares (which is like using If.. Else).

但是,我觉得我应该指出,为了性能,不应该使用这样的结构。当编译器遇到 Select 语句(C# 中的 switch)时,它会尝试使用查找表和switchMSIL 指令编译它们,但在您遇到类似Case 1、2、11、55情况时,编译器将无法转换到查找表,它必须使用一系列比较(就像使用 If.. Else)。

The point is that in order to really take advantage of the Select statement, the cases should be designed with that in mind. Otherwise, the only benefit is code readability.

关键是为了真正利用 Select 语句,案例的设计应该考虑到这一点。否则,唯一的好处是代码可读性。

A well designed switch is an O(1) operation whereas an poorly designed one (which is equivalent to a series of If..Then..Else statements) is an O(n) operation.

设计良好的 switch 是 O(1) 操作,而设计不佳的 switch(相当于一系列 If..Then..Else 语句)是 O(n) 操作。

回答by achinda99

This will allow you to perform "something" in the case of 0, "something else" in the case of 1, "hit" in the case of 2 or 3 or "hit else" otherwise.

这将允许您在 0 的情况下执行“something”,在 1 的情况下执行“something else”,在 2 或 3 的情况下执行“hit”,否则执行“hit else”。

Select Case 2
    Case 0
        Console.WriteLine("something")
    Case 1
        Console.WriteLine("something else")
    Case Is 2 To 3
        Console.WriteLine("hit")
    Else
        Console.WriteLine("hit else")
 End Select