VB.NET Language in a Nutshell

   
Select Case Statement

Syntax

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

testexpression (required; any)

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

expressionlist-n (required; any)

Comma-delimited list of expressions to compare values with testexpression

statements-n (optional)

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

elsestatements (optional)

Program statements to execute if a match between testexpression and any expressionlist cannot 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

Example

The following example uses Select Case to act based on the 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

Programming Tips and Gotchas

See Also

If...Then...Else Statement

   

Категории