CST2601 Visual
Basic I
Notes on Select Case Statement
Select Case StatementExecutes one of several groups of statements, depending on the value of an expression. Syntax Select Case testexpression End Select The Select Case statement syntax has these parts:
Remarks If testexpression matches any Case expressionlist expression, the statements following that Case clause are executed up to the next Case clause, or, for the last clause, up to End Select. Control then passes to the statement following End Select. If testexpression matches an expressionlist expression in more than one Case clause, only the statements following the first match are executed. The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist in any of the other Case selections. 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 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:
Note The Is comparison operator is not the same as the Is keyword used in the Select Case statement. You also can specify ranges and multiple expressions for character
strings. In the following example, Case matches strings that are
exactly equal to
Select Case statements can be nested. Each nested Select Case statement must have a matching End Select statement. Select Case Statement ExampleThis example uses the Select Case statement to evaluate the value of a variable. The second Case clause contains the value of the variable being evaluated, and therefore only the statement associated with it is executed.
|