Home Select Case statements

 
 

The Select Case statement works very much like the If..Then..Else If statement.  However, unlike an If..Then..Else If statement, the Select/Case statement evaluates a single expression.  It is better to use the Select Case statement if you are testing an expression for more than two possibilities. Using a multitude of If statements or if statements with many else if statements can get confusing and lead to errors.  The Select Case provides a simpler structure which is much easier to read.  Case statements are also more efficient.

The Select Case statement starts with a Select Case and ends with an End Select. Its syntax is as follows:

Select Case [tested Expression]

Case [expression list1]
          Statements executed if expression list1 contains a value that matches the tested expression
Case [expression list2]
           Statements executed if expression list2 contains a value that matches the tested expression
Case [expression listN]

The Select Case statement tests a single expression. The result of the expression is checked against values in the Case expression list. If one of the values listed in the Case expression matches the result of the test expression then all statements in that Case clause are executed up to the next Case clause. When a Case clause is run, no other clauses are checked. The next line of code to be run will be the one following the End Select. The Case Else clause is not required but if it is used the statements in the Case Else clause will be executed if none of the other Case expression lists contains a value that matches the tested expression. The case clause can check individual values, ranges and variables or a combination of individual values, ranges, and variables

Case 3, 5, 8 To 12, 23 To 29, intTest

The statements in the Case clause will be executed if the tested expression matches any of the individual values, falls within a range, or is equal to the value stored in the variable intTest.
*Note: Items are separated by commas.
*Note: You must use the word to when giving a range. You can not use 23-29. The smaller number must always be the first number in a range. You can not use 29 to 23.
*Note: You use the Is keyword when using comparison operators (<, <=, >, >=) to specify a range of value. If you do not key in the word Is it will be automatically inserted for you. The Keyword Is in an If statement and the Keyword Is in a Select Case statement are not the same. The Keyword Is can be used in an If statement to compare objects. The Keyword Is in the Select Case statement is used with comparison operators.

Example: Using Select Case

The example below uses the Select Case statement to evaluate the value of the variable intNumber. The value stored in intNumber is 17 which falls in the range given by the clause Case 15 to 19, therefore only the statement in its clause is executed.
   Dim intNumber As Integer
   intNumber = 17
   Select Case intNumber     'intNumber is evaluated
      Case Is < 5     'checks if intNumber is less than 5
         MsgBox("The number is less than 5")

      Case Is > 35     'checks if intNumber is greater than 35
         MsgBox("The number is greater than 35")

      Case 6, 9, 12     'checks if intNumber is 6 or 9 or 12
         MsgBox("The number is 6 or 9 or 12")

      Case 15 To 19     'checks to see if intNumber is between 15 and 19 'inclusive
                        'The variable IntNumber contains a value of 17 which matches the requirement of this clause
                        'This is the only Case clause that evaluates to True.

         MsgBox("The Number is between 15 and 19 inclusive")

      Case Else  'Catch all
                 'If no other clauses are true this statement will be executed

         MsgBox("There is no match for intNumber")

   End Select

Result of Running Program

 Result of Running Select Case program

In the example below, each Case clause evaluates the value of dblSales. If the Case statement is True then all statements within that Case clause are executed.  If for example, the value in dblSales is greater than 8000, the value of 0.11 times dblSales will be placed in the dblCommission variable.
If the value in dblSales falls in the range of 6500 to 8000 inclusive then the value of .10 times dblSales will be placed in the dblCommission variable.
If none of the Case statements are True, the Case Else statement is executed, the value of .5 times dblSales will be placed in the dblCommission variable.

  Select Case dbSales
     Case Is > 8000
          dblCommission = 0.11 * dblSales
     Case 6500 to 8000
          dblCommission = 0.1 * dblSales
     Case Else
          dblCommission = 0.05*dblSales
  End Select

Example: Using Select Case

This program will display the message “You got a hit” if the user enters any of the following: 3, 5, any number between 8 and 12 inclusive, 20, any number between 23 and 29 inclusive and any number greater than 45. The program will display “Number wasn’t found” if the user enters any other number.

Result of Select Case getting a hit

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   Dim intTestNumber As Int32
   intTestNumber = CInt(TextBox1.Text)
   Select Case intTestNumber
      Case 3, 5, 8 To 12, 20, 23 To 29, Is > 45
         MsgBox("You got a hit")
      Case 3, 5, 9    ' clause will never run because it
                      'contains values from the previous clause

         MsgBox("This message will never be displayed")
      Case Else
         MsgBox("Number wasn't found")
   End Select
End Sub
*Note: The second case clause will never be executed because all of its values are contained in the first case clause. After the first case clause statements are executed the program will continue with whatever line follows the End Select statement.

  Using String Ranges and Lists in Case clauses


Case clauses can also test for individual string values or strings that fall in a range:

Select Case strTestName
Case "Johnson", "Knoph" To "Smith", strEmployeeName

All the statements in this Case clause will be executed if the value in strTestName is equal to Johnson, or a name that falls in the alphabetic range of Knoph to Smith such as Lynn, or if there is a name in the strEmployeeName variable that matches what is in strTestName.