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
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)
Calling GetStatus (“Status ok”) in the following procedure
Public Sub btnSelect_Click() GetStatus("Status ok") End ' btnSelect_Click()