User Tools

Site Tools


k10:k10.2:k10.2.3:start

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()

Fehler

Download

10.2.3 Select .. Case .. End Select

Die Syntax für diese Kontrollstruktur mag Sie zuerst mit ihrer Vielfalt überraschen – sie ist aber im praktischen Einsatz schnell umzusetzen:

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 Hinweise

  • Die Notation von Case nach Select ist optional.
  • Es wird ein Ausdruck als Selektor angegeben und dann der Code in der entsprechenden passenden Case-Anweisung ausgeführt.
  • Wenn kein Case-Ausdruck mit dem Selektor übereinstimmt, wird die Default- bzw. Case-Else-Anweisung – wenn vorhanden – ausgeführt.
  • Eine Case-Anweisung ist ein Einzelwert, eine durch Komma getrennte Liste von Einzelwerten oder ein Intervall von Werten, die durch das To-Schlüsselwort getrennt sind.
  • Fallunterscheidungen können Sie auch mit dem Like-Operator realisieren.
  • In ausgewählten Fällen ist die Kontrollstruktur Select..Case..End_Select einer verschachtelten Kontrollstruktur If..Then..Else_If_End_If – bei gleicher Wirkung – vorzuziehen, weil sie dem Quelltext eine gut lesbare Struktur verleiht.

10.2.3.2 Beispiele

Die Beispiele zeigen vorwiegend Quelltext-Ausschnitte und werden kurz kommentiert.

Beispiel 1

Die Auswahl wird für verschiedene (Zahlen-)Bereiche mit unterschiedlichen Selektoren vorgenommen:

Public Sub GetInformation(iNumber As Integer)  
  Select Case iNumber
    Case 0 
      Print "Auswahl = 0"
    Case 1 To 2
      Print "Auswahl 1 oder 2"
    Case 3 To 5, 7 To 9
      Print "Auswahl zwischen 2-5 oder 7-9"
    Case 6
       Print "Auswahl = 6"
    Case Else
      Print "Auswahl nicht im Bereich von 0-9"
    End Select
End ' GetInformation(iNumber As Integer)
 
' Public Sub GetInformation(iNumber As Integer) 
'   Select CStr(iNumber)
'     Case "0" 
'       Print "Auswahl 0"
'     Case Like "[1-2]" 
'       Print "Auswahl 1 oder 2"
'     Case Like "[3-57-9]" ' Case Like "[345789]" 
'       Print "Auswahl zwischen 3-5 oder 7-9"
'      Case "6" 
'       Print "Auswahl 6"
'     Case Else
'       Print "Auswahl nicht im Bereich von 0-9"
'     End Select' 
' End ' GetInformation(iNumber As Integer)

Als Selektoren werden ein Zahlenwert (0 und 6), ein Zahlenbereich 1-2 oder eine Liste von zwei Zahlenbereichen 3-5 und 7-9 eingesetzt. Der auskommentierte Quelltext setzt für die Selektoren den Like-Operator ein.

Mit diesem Quelltext werden nach einem Klick auf den entsprechenden Button

Public Sub btnSelectedRange_Click()
  GetInformation(0)
  GetInformation(2)
  GetInformation(8)
  GetInformation(6)
  GetInformation(13)
End ' btnSelectedRange_Click()

folgende Zeilen in der Konsole der IDE ausgegeben:

Auswahl = 0
Auswahl 1 oder 2
Auswahl zwischen 2-5 oder 7-9
Auswahl = 6
Auswahl nicht im Bereich von 0-9

Beispiel 2

Zuerst wird eine If..Then..Else_If_End_If-Kontrollstruktur für drei alternative Fälle angegeben:

For iNumber = 0 To xmlNode.Children.Count1 
 
    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 

und dann die Umsetzung in eine adäquate Select..Case..End_Select-Kontrollstruktur:

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

Beispiel 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 ' Form_KeyPress() 

Das erste Case prüft eine Liste von Werten. Eine alternative Auswahl existiert bei 'CASE ELSE' nicht – ist aber vorgesehen!

Beispiel 3

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)
  • Dem Select in der zweiten Zeile folgt hier kein Case, weil dessen Angabe optional ist.
  • Die Fallunterscheidungen werden in den beiden regulären Fällen über den Like-Operator (→ Kapitel 8.3.3 LIKE) umgesetzt.
  • Die Aufbereitung der Meldungen wird auf unterschiedliche Weise vorgenommen.
  • Die Fehlermeldung wird nur dann angezeigt, wenn keiner der beiden regulären Fälle zutrifft.

Der Aufruf von GetStatus(“Status ok”) in der folgenden Prozedur

Public Sub btnSelect_Click()
  GetStatus("Status ok")
End ' btnSelect_Click()

erzeugt diese Fehler-Meldung in einem separaten Fenster:

Fehler

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k10/k10.2/k10.2.3/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools