Vb Flexible Select Case

SwitchStatement for VisualBasic (both VbClassic and VisualBasicDotNet)

[See ElseIfIsSelectCase for languages with stricter limitations on select-case syntax.]


VB's select case is much more flexible if used like this:

 Select Case True
   Case <boolean expr 1>
     <block 1>
   Case <boolean expr 2>
     <block 2>
   Case <boolean expr 3>
     <block 3>
   Case True
     <default block>
 End Select
In fact this method lets you do range comparisons, call functions, or test several values, just like you wanted.


The original sample from ElseConsideredHelpful rewritten like this becomes:

 Private Sub WritePropertyElement(ByVal psName As String, ByVal pvValue As Variant)
    Dim xmlElmt As MSXML.IXMLDOMElement
    ' ...  Code to create the XML Element and write pvValue type information into it.  ...
    Select Case True
    Case IsDate(pvValue)
       xmlElmt.Text = Format$(pvValue, "YYYY/MM/DD HH:NN:SS")
    Case VarType(pvValue) = vbObject
       If pvValue Is Nothing Then
          ' (No text)
       Else
          Call DoObjectSerialization(xmlElmt, pvValue)
       End If
    Case IsNull(pvValue)
       ' (No text)
    Case IsEmpty(pvValue)
       ' (No text)
    Case IsMissing(pvValue)
       xmlElmt.Text = "Missing"
    Case True
       xmlElmt.Text = CStr(pvValue)
    End Select
 End Sub

Another actual code sample:

 Private Function IsProcedureHeader( aLine As String ) As Boolean
   IsProcedureHeader = True
   Select Case True
    Case BeginsWith(UCase$("private sub"), UCase$(aLine))
    Case BeginsWith(UCase$("public sub"), UCase$(aLine))
    Case BeginsWith(UCase$("friend sub"), UCase$(aLine))
    Case BeginsWith(UCase$("sub"), UCase$(aLine))
    Case BeginsWith(UCase$("private function"), UCase$(aLine))
    Case BeginsWith(UCase$("public function"), UCase$(aLine))
    Case BeginsWith(UCase$("friend function"), UCase$(aLine))
    Case BeginsWith(UCase$("function"), UCase$(aLine))
    Case BeginsWith(UCase$("private property"), UCase$(aLine))
    Case BeginsWith(UCase$("public property"), UCase$(aLine))
    Case BeginsWith(UCase$("friend property"), UCase$(aLine))
    Case BeginsWith(UCase$("property"), UCase$(aLine))
    Case True
      IsProcedureHeader = False
   End Select
 End Function

Private Function BeginsWith( aBeginning As String, aText As String ) As Boolean BeginsWith = (Left$(aText, Len(aBeginning)) = aBeginning) End Function
(Good example. I'd probably move "UCase$()" into the "BeginsWith()" or make a new function that does this. -- JeffGrigg)


One can also use direct values instead of expressions.

    select case ucase(rs("fmtType"))
      case "T","N","D"    ' text, number, or date
        hout inputBox("text", fldRef, useValue, useWidth, useWidth)
      case "Y"    ' Boolean
        temp = trim(lcase(rs("fldValue")) & "")
        if isBlank(temp) or temp="(either)" then
          useValue = "(either)"
        elseif contains("1,true,yes,on", temp) then
          useValue = "Yes"
        else
          useValue = "No"
        end if
        hout pickList1(fldRef, useValue, "(either),Yes,No")
      case "L"    ' List
        hout pickList1(fldRef, useValue, "(any)," & rs("theList"))
    end select


You can do ranges in VB Case . . .

a specific line from a program I am writing at this moment:

  Case WM_SETCURSOR, WM_NCHITTEST, WM_MOUSEMOVE, &HA0 To &HA9
selects based on one of three values, or a range.

--NeilEBryant


You can also do...

    Select Case expression
        Case Is > 5
            ' Code Here
        ...etc...
    End Select


CategoryVisualBasic, CategoryConditionalsAndDispatching


EditText of this page (last edited February 4, 2009) or FindPage with title or text search