Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is the syntax for select case?

Select Case statements can be nested. Each Select Case statement must have a matching End Select statement. The expression list that follows the Case in a Select Case statement must be a list of constant numeric or string expressions. If you want to use variables for different cases, use the If...Then...

What is Select Case statement in computer?

A SELECT CASE statement, often referred to as a CASE statement, is used to compare a given value with preselected constants and take an action according to the first constant to match. A CASE statement can be handy to select between a series of different possibilities or cases.

Why is the Select Case statement also known as a conditional statement?

Select Case is a conditional statement, that helps you test a variable for equality against a set of values. Each value is referred to as a case, and a variable that is being switched on should be checked for all the select cases.

How do you write switch statement in VB?

To execute a group of statements depending upon the value of an Expression, then we use the Switch Case. Here, each value is called a Case, and the variable is being switched ON based on each case. Else statement case is executed if the test expression doesn't match with any of the Case specified by the user.


Use the comma operator to delimit case statements

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

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

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


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

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

Here's the MSDN


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.

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.

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 switch MSIL instruction but in the case where you have something like Case 1,2,11,55 the 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).

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.

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.


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.

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