The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Select...Case Statements

Visual Studio .NET 2003

When you are comparing the same expression to several different values, you can use the Select...Case statements as an alternative to the If...Then...Else statements. Whereas the If and ElseIf statements can evaluate a different expression in each statement, the Select statement evaluates a single expression only once and uses it for every comparison.

Visual Basic compares the value of the expression to the values in the Case statements in the order they appear in the Select...Case block. If it finds a match or a Case Else statement, it executes the corresponding statement block. In any case, it then executes the code following the End Select statement.

You can have any number of Case statements, and you can include or omit a Case Else statement.

In the following example, Select...Case is used to evaluate the performance rating that is passed to the Function procedure. Note that each Case statement can contain more than one value, a range of values, or a combination of values and comparison operators. When a Case statement contains more than one value, the Case block is executed if any of the values match the value of the Select statement expression.

Function Bonus(ByVal Performance As Integer, ByVal Salary As Decimal) _
      As Decimal
   Select Performance
     Case 1   ' Performance is 1.
        Return Salary * 0.1
     Case 2, 3   ' Performance is 2 or 3.
        Return Salary * 0.09
     Case 5 To 7   ' Performance is 5, 6, or 7.
        Return Salary * 0.07
     Case 4, 8 To 10   ' Performance is 4, 8, 9, or 10.
        Return Salary * 0.05
     Case Is < 15   ' Performance is 11, 12, 13, or 14.
        Return 100
     Case Else   ' Performance is < 1 or > 14.
        Return 0
   End Select
End Function

See Also

Loop Structures | Nested Control Statements | Select...Case Statements (Language Reference) | If...Then...Else Statements | Try...Catch...Finally Statements | Exit Statement

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft