Difference between revisions of "Select Case"

From Xojo Documentation

m (1 revision)
m
 
(42 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
{{KeywordBox
 +
| name=Select Case
 +
| platform=all
 +
| scope=global
 +
}}
 +
{{Description
 +
|text = Executes one of several groups of statements, depending on the value of an expression. }}
  
 +
== Usage ==
 +
'''Select Case''' ''testExpression''<br/>
 +
['''Case''' ['''Is''']  ''expression-n'' [''statements-n'']<br/>
 +
'''Else or Case Else''' <br>''elseStatements'']<br/>
 +
'''End''' ['''Select''']
  
=='''Description'''==
+
{| class="genericTable"
Executes one of several groups of statements, depending on the value of an expression.
 
  
 +
! width=15% | Part
  
 
+
! width=55% | Description
=='''Syntax'''==
 
'''Select Case''' '''testExpression'''['''Case''' [Is]  <div style="font-style:italic; color:green;">Introduced 5.5</div>'''expression-n''''''statements-n''']
 
['''<div style="font-style:italic; color:green;">Changed 2005r1</div>Else ''''''or'''''' Case Else''''''elseStatements''']
 
'''End''' ['''Select''']
 
{| cellpadding="8" cellspacing="0" border="1"
 
 
 
! width=25%  style="background-color:#e0e0e0" | Part
 
 
 
! width=55% style="background-color:#e0e0e0" | Description
 
 
|-
 
|-
 
|testExpression
 
|testExpression
  
|<div style="font-style:italic; color:green;">Changed 5.5</div>Any expression that evaluates to a value. The expression can be of any data type or an object.
+
|Any expression that evaluates to a value. The expression can be of any data type or an object.<div style="font-style:italic; color:green;">Changed 5.5</div>
  
 
|-
 
|-
|<div style="font-style:italic; color:green;">Changed 5.5</div>expression-n
+
|expression-n<div style="font-style:italic; color:green;">Changed 5.5</div>
  
|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|Is]] keyword to do an equality or inequality test, or an expression that uses [[IsA|IsA]] to determine the data type of an object.  
+
|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.  
  
 
|-
 
|-
Line 33: Line 36:
 
|elseStatements
 
|elseStatements
  
|Statements to be executed if no expressions are [[True|True]].
+
|Statements to be executed if no expressions are [[True]].
  
 
|-
 
|-
 
|}
 
|}
  
 
+
== Notes ==
=='''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 '''Select Case''' statement is useful when there are several possible conditions that must be checked. Unlike an [[If|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 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.
  
<div style="font-style:italic; color:green;">Changed 2008r3</div>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|Is]] keyword to do an equality or inequality test, or an expression that uses [[IsA|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:
  
Here are some examples:
+
<rbcode>
{| cellpadding="8" cellspacing="0" border="1"
+
Case 2, 4, 6, 8 // several values
|<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' 2, 4, 6, 8 //several values
+
Case 2 To 5 // range of values using To
<br /></div>
+
Case 2 To 5, 7, 9, 11 // Both separate values and range
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' 2 '''To''' 5 //range of values using To
+
Case myFunction(x) // a Function
<br /></div>
+
Case Is >= 42 // greater than/equal to operator
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' 2 '''To''' 5, 7,9,11 //Both separate values and range
+
Case Is < 19 // less than operator
<br /></div>
+
Case IsA PushButton // tests whether an object is a pushbutton
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' myFunction(x) // a Function
+
</rbcode>
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' '''Is''' &gt;= 42 // greater than/equal to operator
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' '''Is''' &lt;19 //less than operator
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;Case [[IsA|IsA]] [[PushButton|PushButton]]  //tests whether an object is a pushbutton
 
<br /></div>
 
  
|-
+
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:
|}
 
  
You can declare local variables using the [[Dim|Dim]] 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:
+
<rbcode>
{| cellpadding="8" cellspacing="0" border="1"
+
Select Case dayNumber
|<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Select Case''' dayNumber
+
Case 2
<br /></div>
+
  Var day As String
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' 2
+
  day = "Tuesday"
<br /></div>
+
Else
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Dim|Dim]] day as [[String|String]]<br /></div>
+
  MessageBox("It's not Tuesday!")
<div style="width:100%; background-color:#d0d0d0; padding-left:30px"> day="Tuesday"
+
End Select
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Else'''<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[MsgBox|MsgBox]] "It's not Tuesday!"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''End Select'''<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[MsgBox|MsgBox]] day //day is out of scope
 
<br /></div>
 
  
|-
+
MessageBox(day) // day is out of scope here
|}
+
</rbcode>
  
 
The variable "day" should be declared prior to the Select...Case statement so that it is available after the End Select statement executes.
 
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.
  
 +
<rbcode>
 +
Var dayNumber As Integer
 +
Var msg As String
 +
dayNumber = Val(TextField1.Value)
  
=='''Examples'''==
+
Select Case dayNumber
This example uses the '''Select Case''' statement to determine which day of the week the user has entered into a [[TextField|TextField]] and displays a message based on that information.
+
Case 2
{| cellpadding="8" cellspacing="0" border="1"
+
  msg = "It's Monday"
|<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Dim|Dim]] dayNumber As [[Integer|Integer]]<br /></div>
+
Case 3
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Dim|Dim]] msg As [[String|String]]<br /></div>
+
  msg = "It's Tuesday"
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">dayNumber=[[Val|Val]]([[TextField|TextField1]].text)
+
Case 4
<br /></div>
+
  msg = "It's Wednesday"
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Select Case''' dayNumber
+
Case 5
<br /></div>
+
  msg = "It's Thursday"
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' 2
+
Case 6
<br /></div>
+
  msg = "It's Friday"
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;msg="It's Monday"
+
Else
<br /></div>
+
  msg = "It's the weekend."
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' 3
+
End Select
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;msg="It's Tuesday"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' 4
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;msg="It's Wednesday"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' 5
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;msg="It's Thursday"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' 6
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;msg="It's Friday"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Else'''<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;msg="It's the weekend."
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''End Select'''<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[MsgBox|MsgBox]] msg
 
<br /></div>
 
 
 
|-
 
|}
 
  
The following example uses the '''Select Case''' statement to determine which button was pressed in a [[MessageDialog|MessageDialog]]. Notice that it compares objects of type [[MessageDialogButton|MessageDialogButton]].
+
MessageBox(msg)
{| cellpadding="8" cellspacing="0" border="1"
+
</rbcode>
|<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Dim|Dim]] d as [[New|New]] [[MessageDialog|MessageDialog]]  //declare the MessageDialog object
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Dim|Dim]] b as [[MessageDialogButton|MessageDialogButton]] //for handling the result
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">d.icon=[[MessageDialog|MessageDialog]].GraphicCaution //display warning icon
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">d.ActionButton.Caption="Save"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">d.CancelButton.Visible=[[True|True]]  //show the Cancel button
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">d.CancelButton.Cancel=[[True|True]]  //esc key works for Cancel
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">d.AlternateActionButton.Visible=[[True|True]]   //show the "Don't Save" button
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">d.Message="Save changes before closing?"
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">d.Explanation="If you don't save your changes, you will lose "[[_|_]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;    +"all that important work you did since your last coffee break."
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">b=d.ShowModal  //display the dialog
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Select Case''' b  //b is a MessageDialogButton
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' d.ActionButton //determine which button was pressed.
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;//user pressed Save
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' d.AlternateActionButton
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;//user pressed Don't Save
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''Case''' d.CancelButton
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;&#xA0;//user pressed Cancel
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">'''End Select'''<br /></div>
 
  
|-
+
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]].
|}
 
  
 +
<rbcode>
 +
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
  
=='''See Also'''==
+
Select Case b // b is a MessageDialogButton
[[If|If...Then...Else]] statement; [[IsA|IsA]] operator.
+
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
 +
</rbcode>
  
 +
== See Also ==
 +
[[If...Then...Else]] statement; [[IsA]] operator.
  
[[Category:Code_Execution]]
+
[[Category:Language_Code_Execution]]

Latest revision as of 20:27, 22 November 2019

Language Keyword

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, 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 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:

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 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 dayNumber As Integer
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 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

See Also

If...Then...Else statement; IsA operator.