Keyword

# Select Case

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Executes one of several groups of statements, depending on the value of an expression.

## Usage

``` xojo
Select Case testExpression
[ Case [ Is | IsA ] 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.                                                                                                                                                                                                                                                                                                                                                                                                                         |
| expression-n   | 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</api/language/operators/comparison/is>` keyword to do an equality or inequality test, or an expression that uses `IsA</api/language/operators/comparison/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</api/language/true>`.                                                                                                                                                                                                                                                                                                                                                                                                                                            |

## Notes

When the *testExpression* is a `String</api/data_types/string>`, comparisons are case-insensitive.

The <span class="title-ref">Select Case</span> statement is useful when there are several possible conditions that must be checked. Unlike an `If</api/language/if>` statement, the <span class="title-ref">Select Case</span> statement will exit as soon as it finds a matching <span class="title-ref">Case</span> expression and executes any statements that follow the <span class="title-ref">Case</span> expression up to the next <span class="title-ref">Case</span> expression. If there are no <span class="title-ref">Case</span> expressions that match, the elseStatements are executed.

The expression <span class="title-ref">Case Else</span> can be used as a synonym for Else.

The <span class="title-ref">Case</span> 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</api/language/operators/comparison/is>` keyword to do an equality or inequality test, or an expression that uses `IsA</api/language/operators/comparison/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:

``` xojo
Case 2, 4, 6, 8 ' several values
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 DesktopButton ' tests whether an object is a DesktopButton
```

You can declare local variables using the `Var</api/language/var>` statement inside a <span class="title-ref">Case</span> statement. Such variables will be local to the <span class="title-ref">Select Case</span> statement and will, therefore, be out of scope after the End Select statement executes. For example:

``` xojo
Select Case dayNumber
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 <span class="title-ref">Select Case</span> statement to determine which day of the week the user has entered into a `TextField</api/user_interface/desktop/desktoptextfield>` and displays a message based on that information.

``` xojo
Var dayNumber As Integer
Var msg As String
dayNumber = TextField1.Text.Val

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 <span class="title-ref">Select Case</span> statement to determine which button was pressed in a `MessageDialog</api/user_interface/desktop/messagedialog>`. Notice that it compares objects of type `MessageDialogButton</api/user_interface/desktop/messagedialogbutton>`.

``` xojo
Var d As New MessageDialog ' declare the MessageDialog object
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
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`If...Then...Else</api/language/if...then...else>` statement; `IsA</api/language/operators/comparison/isa>` operator.

</div>
