Table of Contents
10.2.3 Select... case End Select
The syntax for this control structure may surprise you with its variety - but it can be implemented quickly in practical use:
SELECT [ CASE ] Expression
[ CASE [ Expression ] [ TO Expression #2 ] [ , ... ]
... ]
[ CASE [ Expression ] [ TO Expression #2 ] [ , ... ]
... ]
[ CASE LIKE Expression [ , ... ]
... ]
[ { CASE ELSE | DEFAULT }
... ]
END SELECT
10.2.3.1 Notes
- The notation from Case to Select is optional.
- An expression is specified as a selector and then the code is executed in the appropriate case statement.
- If no case expression matches the selector, the system executes the default or case Else statement (if available).
- A case statement is a single value, a comma-separated list of single values, or an interval of values separated by the To keyword.
- You can also make case distinctions with the Like operator.
- In selected cases, the control structure is Select.. Case.. End_Select of a nested control structure If… Then… Else_If_End_If - with the same effect - is preferable, because it gives the source code an easily readable structure.
10.2.3.2 Examples
The examples mainly show source code excerpts and are commented on briefly.
Example 1
The selection is made for different (number ranges) with different selectors:
Public Sub GetInformation(iNumber As Integer) Select Case iNumber Case 0 Print "Selection = 0" Case 1 To 2 Print "Selection 1 or 2" Case 3 To 5, 7 To 9 Print "Choose between 2-5 or 7-9" Case 6 Print "Selection = 6" Case Else Print "Selection not in range 0-9" End Select End ' Public Sub GetInformation(iNumber As Integer) ' Select CStr(iNumber) ' Case "0" ' Print "Selection 0" ' Case Like "[1-2]" ' Print "Selection 1 or 2" ' Case Like "[3-57-9]" ' Case Like "[345789]" ' Print "Choose between 3-5 or 7-9" ' Case "6" ' Print "Selection 6" ' Case Else ' Print "Selection not in range 0-9" ' End Select' ' End
A numerical value (0 and 6), a numerical range 1-2 or a list of two numerical ranges 3-5 and 7-9 are used as selectors. The commented out source code uses the Like operator for the selectors.
With this source code after clicking on the corresponding button
Public Sub btnSelectedRange_Click() GetInformation(0) GetInformation(2) GetInformation(8) GetInformation(6) GetInformation(13) End ' btnSelectedRange_Click()
the following lines in the console of the IDE:
Selection = 0 Selection 1 or 2 Choice between 2-5 or 7-9 Selection = 6 Selection not in range 0-9
Example 2
First, specify an If..Then..Else_If_End_If control structure for three alternative cases:
For iNumber = 0 To xmlNode.Children.Count - 1 sBuffer = xmlNode.Children[iNumber].Name If sBuffer = "title" Then sTitle = xmlNode.Children[iNumber].Value Else If sBuffer = "link" Then sItemLink = xmlNode.Children[iNumber].Value Else If sBuffer = "description" Then sDescription = xmlNode.Children[iNumber].Value Endif Next
and then the implementation into an adequate Select.Case..End_Select control structure:
For iNumber = 0 To xmlNode.Children.Count - 1 sBuffer = xmlNode.Children[iNumber].Name Select Case sBuffer Case "title" sTitle = xmlNode.Children[iNumber].Value Case "link" sItemLink = xmlNode.Children[iNumber].Value Case "description" sDescription = xmlNode.Children[iNumber].Value End Select Next
Example 3
PUBLIC SUB Form_KeyPress() IF Key.Control SELECT CASE Workspace1.Children.Find(Workspace1.ActiveWindow) CASE 0 TO Workspace1.Children.Count - 2 Workspace1.ActiveWindow = Workspace1.Children[Workspace1.Children.Find(Workspace1.ActiveWindow) + 1] CASE Workspace1.Children.Count - 1 Workspace1.ActiveWindow = Workspace1.Children[0] CASE ELSE ' Alternative ... END SELECT ENDIF END
The first case checks a list of values. There is no alternative selection for 'CASE ELSE' - but it is planned!
Example 4
Public Sub GetStatus(sStatus As String) Select sStatus Case Like "[+Pp]*" Message.Info(IIf(sStatus Begins "+", "Status: positiv", "Status: " & sStatus)) Case Like "[-Nn]*" If sStatus Begins "-" Then sStatus = "negativ" Message.Info("Status: " & sStatus) Default Message.Error("Der Status konnte NICHT ermittelt werden!") End Select End ' GetStatus(sStatus As String)
- The select in the second line is not followed by a case here, because its specification is optional.
- The case differentiations are implemented in the two regular cases using the Like operator (? chapter 8.3.3 LIKE).
- The preparation of the messages is done in different ways.
- The error message is only displayed if neither of the two regular cases applies.
Calling GetStatus (“Status ok”) in the following procedure
Public Sub btnSelect_Click() GetStatus("Status ok") End ' btnSelect_Click()

