Select Case Statement

Syntax

Select Case testexpression
   [Case expressionlist-n
      [statements-n]] ...
   [Case Else
      [elsestatements]]
End Select


testexpression

Use: Required

Data Type: Any

Any numeric or string expression whose value determines which block of code is executed.


expressionlist-n

Use: Required

Data Type: Any

Comma-delimited list of expressions to compare values with testexpression.


statements-n

Use: Optional

Program statements to execute if a match is found between any section of expressionlist and testexpression.


elsestatements

Use: Optional

Program statements to execute if a match between testexpression and any expressionlist can't be found.

expressionlist can use any (or a combination of any) of the following:

expressionlist Syntax Examples
expression
iVar - iAnotherVar 
iVar

expression To expression
5 To 10 
8 To 11, 13 to 15
"A" To "D"

Is comparisonoperator
											expression

Is = 10


Description

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

Rules 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 expressionlist is found, the program statements following the matched expressionlist are 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 used, the Case Else clause must be the last Case clause. Program execution encounters the Case Else clause—and thereby executes, the elsestatements—only if 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 follow it. Failure to do this doesn't generate a syntax error; instead, it causes the comparison of the expression with testexpression to always fail, so that program execution falls through to the Case Else code block, if one is present.

  • The Is keyword precedes any comparison operators.

  • Select Case statements can also be nested, resulting in a successful match between testexpression and expressionlist being another Select Case statement.

Example

The following example uses Select Case to implement the click event handler for a menu control array—that is, several menu options with the same name and different index numbers.

Private Sub mnuOption_Click(Index As Integer)

Select Case Index
   Case Is = 0
      Call ShowAddNewForm
   Case Is = 1
      Call ShowEditForm
   Case Is = 2
      Call ShowDeleteForm
   Case Else
      MsgBox "Not a valid menu option"
End Select

End Sub

Programming Tips and Gotchas

  • The Select Case statement is the VB equivalent of the Switch construct found in C and C++.

  • The Case Else clause is optional. However, as with If...Then...Else statements, it's often good practice to provide a Case Else to catch the exceptional instance where—perhaps unexpectedly—a match can't be found in any of the expressionlists you have provided.

  • The To clause can specify ranges of character strings. However, it's often difficult to predict the thousands of possible combinations of valid characters between two words that are successfully matched by Select Case.

  • The Is keyword used in the Select Case statement isn't the same as the Is comparison operator.

  • Multiple conditions in a single Case statement are evaluated separately, not together; that is, they are connected with a logical OR, not a logical AND. For example, the statement

    Case Is > 20, Is < 40

    evaluates to True whenever the value of testexpression is greater than 20. In this case, the second comparison is never evaluated; it's evaluated only when testexpression is under 20. This suggests that if you use anything other than the most straightforward conditions, you should test them thoroughly.

See Also

If...Then Statement
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.175.151