Transcrevemos abaixo o Help do Visual Studio para este comando:
Executes one of several groups of statements, depending on the value of an expression.
Select [ Case ] testexpression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
Required. Expression. Must evaluate to one of the elementary data types (Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, Short, Single, and String).
Required in a Case statement. List of expression clauses representing match values for testexpression. Multiple expression clauses are separated by commas. Each clause can take one of the following forms:
· expression1 To expression2
· [ Is ] comparisonoperator expression
Use the To keyword to specify the boundaries of a range of match values for testexpression. The value of expression1 must be less than or equal to the value of expression2.
Use the Is keyword with a comparison operator (=, <>, <, <=, >, or >=) to specify a restriction on the match values for testexpression. If the Is keyword is not supplied, it is automatically inserted before comparisonoperator.
The form specifying only expression is treated as a special case of the Is form where comparisonoperator is the equal sign (=). This form is evaluated as testexpression = expression.
The expressions in expressionlist can be of any data type, provided they are implicitly convertible to the type of testexpression and the appropriate comparisonoperator is valid for the two types it is being used with.
Optional. One or more statements following Case that are executed if testexpression matches any clause in expressionlist.
Optional. One or more statements following Case Else that are executed if testexpression does not match any clause in the expressionlist of any of the Case statements.
Terminates Select...Case block.
If testexpression matches any Case expressionlist clause, the statements following that Case statement are executed up to the next Case statement or the End Select statement. Control then passes to the statement following End Select. If testexpression matches an expressionlist clause in more than one Case clause, only the statements following the first match are executed.
The Case Else statement is used to introduce the elsestatements to be executed if no match is found between the testexpression and an expressionlist clause in any of the other Case statements. Although not required, it is a good idea to have a Case Else statement in your Select Case block to handle unforeseen testexpression values. If no Case expressionlist clause matches testexpression and there is no Case Else statement, execution continues at the statement following End Select.
You can use multiple expressions or ranges in each Case clause. For example, the following line is valid:
Case 1 To 4, 7 To 9, 11, 13, Is > MaxNumber
Note The Is keyword used in the Case and Case Else statements is not the same as the Is comparison operator.
You also can specify ranges and multiple expressions for character strings. In the following example, Case matches strings that are exactly equal to "apples", strings with values between "nuts" and "soup" in alphabetical order, and the current value of TestItem:
Case "apples", "nuts" To "soup", TestItem
The setting of Option Compare can affect string comparisons. Under Option Compare Text, the strings "Apples" and "apples" compare as equal, but under Option Compare Binary, they do not.
Select Case statements can be nested. Each nested Select Case statement must have a matching End Select statement.
If you do not need to execute any more of the statements in a Case or Case Else statement block, you can exit the block by using the Exit Select statement. This transfers control immediately to the statement following End Select.
This example uses the Select Case statements to write a line corresponding to the value of the variable Number. The second Case statement contains the value that matches the current value of Number, so the statement that writes "Between 6 and 8" is executed.
Dim Number As Integer = 8
Select Number ' Evaluate Number.
Case 1 To 5 ' Number between 1 and 5, inclusive.
Debug.WriteLine("Between 1 and 5")
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8 ' Number between 6 and 8.
Debug.WriteLine("Between 6 and 8")
Case 9 To 10 ' Number is 9 or 10.
Debug.WriteLine("Greater than 8")
Case Else ' Other values.
Debug.WriteLine("Not between 1 and 10")