Select Case Statement


Select Case Statement

Syntax

     Select Case testExpression        [Case expressionList-n           [statements-n]] ...        [Case Else           [elseStatements]]     End Select 


testExpression (required)

Any expression whose value determines which block of code within the larger Select Case statement is executed. The expression must evaluate to one of the core Visual Basic data types or Object.


expressionList-n (required)

Comma-delimited list of expressions to compare with testExpression. Each comma-delimited part uses one syntax from the following table:

Expression list syntax

Description

 expression 

testExpression is compared with expression. If they match, the related statements are executed.

 expression1 To expression2 

testExpression is compared with an inclusive range from expression1 to expression2. If they match, the related statements are executed.

 Is op expression 

testExpression is compared with expression by using a specific comparison operator. If they match, the related statements are executed. op may be one of the following comparison operators: =, <>, <, <=, >, or >=. If "Is =" is used, it can be replaced with just "Is."



statements-n (optional)

Program statements to execute if a match is found between testExpression and the related expressionList entry.


elseStatements (optional)

Program statements to execute if none of the other expressionList sections resulted in a match.

Description

The Select Case statement allows for conditional execution of code, typically out of three or more available code blocks, based on some condition. Use the Select Case statement as an alternative to complex If...Then...Else statements.

Usage at a Glance

  • Any number of Case clauses can be included in the Select Case statement.

  • If a match between testExpression and any part of a particular expressionList is found, the program statements related to the matched expressionList will be executed. When program execution encounters the next Case clause or the End Select clause, execution continues with the statement immediately following the End Select clause.

  • If multiple Case clauses are true, only the statements belonging to the first true Case clause are executed.

  • If used, the Case Else clause must be the last Case clause. Program execution will only encounter the Case Else clauseand thereby execute the elseStatements sectionif all other expressionList comparisons fail.

  • Use the To keyword to specify a range of values. The lower value must precede the To clause, and the higher value must follow it. Failure to do this does not generate a syntax error. Instead, it causes the comparison of the expression with testExpression to always fail, so that the related section of code is never executed.

  • Select Case statements can be nested.

  • The Case Else clause is optional, but it should be included if you must take some action when all other Case clauses fail.

  • The Is keyword used in the Select Case statement is not the same as the Is comparison operator.

  • Multiple conditions in a single Case statement are evaluated separately, not together; they are connected with a logical Or, not a logical And. For example, the statement:

         Case Is > 20, Is < 40 

    will evaluate to TRue whenever the value of testExpression is greater than 20. In this case, the second comparison is never evaluated; it is evaluated only when testExpression is under 20. A testExpression value of 60 evaluates to true in this case.

Example

The following example uses Select Case in response to a MsgBox function:

     Select Case MsgBox("Backup file before changing?", vbYesNoCancel)        Case vbYes           ' ----- Do something.        Case vbNo           ' ----- Do something.        Case vbCancel           ' ----- Do something.     End Select 

See Also

If...Then...Else Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net