Select Case
From Xojo Documentation
Executes one of several groups of statements, depending on the value of an expression.
Usage
Select Case testExpression
[Case [Is] expression-n [statements-n]
Else or Case Else
elseStatements]
End [Select]
Part | Description |
---|---|
testExpression | Any expression that evaluates to a value. The expression can be of any data type or an object. Changed 5.5
|
expression-n Changed 5.5
|
Any expression or list of expressions. You can use a function that evaluates to the data type of testExpression. The expression can be a single value, a comma-delimited list of values, a function that returns a value, a range of values specified with the "To" keyword, an expression that uses the Is keyword to do an equality or inequality test, or an expression that uses IsA to determine the data type of an object. |
statements-n | Statements to be executed if expression-n is true. |
elseStatements | Statements to be executed if no expressions are True. |
Notes
The Select Case statement is useful when there are several possible conditions that must be checked. Unlike an If statement, the Select Case statement will exit as soon as it finds a matching Case expression and executes any statements that follow the Case expression up to the next Case expression. If there are no Case expressions that match, the elseStatements are executed.
The expression Case Else can be used as a synonym for Else.
The Case statement can accept several types of expressions. The expression can be a single value, a comma-delimited list of values, a function that returns a value, a range of values specified with the "To" keyword, an expression that uses the Is keyword to do an equality or inequality test, or an expression that uses IsA to determine whether an object is a member of a particular class. You can combine types of expressions, separating them by commas.
Here are some examples:
Case 2 To 5 // range of values using To
Case 2 To 5, 7, 9, 11 // Both separate values and range
Case myFunction(x) // a Function
Case Is >= 42 // greater than/equal to operator
Case Is < 19 // less than operator
Case IsA PushButton // tests whether an object is a pushbutton
You can declare local variables using the Var statement inside a Case statement. Such variables will be local to the Select Case statement and will, therefore, be out of scope after the End Select statement executes. For example:
Case 2
Var day As String
day = "Tuesday"
Else
MessageBox("It's not Tuesday!")
End Select
MessageBox(day) // day is out of scope here
The variable "day" should be declared prior to the Select...Case statement so that it is available after the End Select statement executes.
Sample Code
This example uses the Select Case statement to determine which day of the week the user has entered into a TextField and displays a message based on that information.
Var msg As String
dayNumber = Val(TextField1.Value)
Select Case dayNumber
Case 2
msg = "It's Monday"
Case 3
msg = "It's Tuesday"
Case 4
msg = "It's Wednesday"
Case 5
msg = "It's Thursday"
Case 6
msg = "It's Friday"
Else
msg = "It's the weekend."
End Select
MessageBox(msg)
The following example uses the Select Case statement to determine which button was pressed in a MessageDialog. Notice that it compares objects of type MessageDialogButton.
Var b As MessageDialogButton // for handling the result
d.Icon = MessageDialog.GraphicCaution // display warning icon
d.ActionButton.Caption = "Save"
d.CancelButton.Visible = True // show the Cancel button
d.CancelButton.Cancel = True // esc key works for Cancel
d.AlternateActionButton.Visible = True // show the "Don't Save" button
d.Message = "Save changes before closing?"
d.Explanation = "If you don't save your changes, you will lose "_
+ "all that important work you did since your last coffee break."
b = d.ShowModal // display the dialog
Select Case b // b is a MessageDialogButton
Case d.ActionButton // determine which button was pressed.
// user pressed Save
Case d.AlternateActionButton
//user pressed Don't Save
Case d.CancelButton
// user pressed Cancel
End Select
See Also
If...Then...Else statement; IsA operator.