Select...Case...End Select Statements

Evaluates a target expression, compares its value to a series of values and executes the block of statements associated with the first matching value.

Selectmatch_value
   Casetest_expression, ..., test_expression
     case_statements
[ Casetest_expression, ..., test_expression
     [case_statements]]
         :
[ Case Else
     [else_statements]]
End Select

Prerequisites

None

Parameters

match_value

Required numeric or String expression that defines the value to be matched.

test_expression

Required numeric or String expression that is specified with each Case statement to define the values to be compared to the match_value.  Each Case statement must have at least one test_expression, but can have more than one.

case_statements

Optional statement or list of statements that are executed if any of the test_expressions for the associated Case statement match the match_value.

else_statements

Optional statement or list of statements that are executed if the Case Else statement is present and none of the test_expressions match the match_value.

Remarks

This control structure executes one of several blocks of statements based upon matching a numeric or String expression value.  This control structure is similar to the If…Then...ElseIf statements in that a series of values are compared to determine the statements that are executed next.  However, this control structure is more efficient and convenient than a series of If statements if a single value is to be compared to multiple possible values.

The Select statement defines the value to be matched.  The match_value is evaluated once and then sequentially tested against each test_expression specified in the following Case statements. When the first matching test_expression value is found, the associated case_statements are executed.  Following the execution of the appropriate case_statements, execution continues at the statement following the End Select. If no test_expression is matched and a Case Else is present, the else_statements are executed.  If no test_expression is matched and a Case Else is not defined, none of the case_statements are executed and execution continues after the End Select

The match_value and each of the test_expressions can be either a numeric or String expression and can evaluate to any of the basic arithmetic data types (e.g. integer, real number, byte) or a String type.  If the data type of a test_expression does not match that of the match_value, it is automatically converted to the correct data type.  If a String comparison is performed, the comparison is case sensitive, e.g. "A" and "a" are considered different.

A Select sequence must contain at least one Case or Case Else statement.  Any number of additional Case statements can be included, but only one Case Else is permitted and the Case Else must occur just prior to the End Select.

If an Exit Select is encountered in either the case_statements or else_statements, execution of the remaining statements in the block is skipped.  Execution continues at the instruction following the End Select.

Examples

Dim target, s1, s2 As String
target = "ab"
s1 =
"a"
s2 =
"b"
Select
target
Case s1, "dd"
Console.Writeline("Wrong")
Case s2
Console.Writeline("Wrong")
Case s1 & s2
Console.Writeline("Right")
Case Else
Console.Writeline("Wrong")
End Select

See Also

Statements|Do… LoopStatements | For…NextStatements | GoToStatements | If…Then…Else…End IfStatements | While…End WhileStatements