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

Case

Case

Case True

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 W'ritePropertyElement(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 D'oObjectSerialization(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 I'sProcedureHeader( aLine As String ) As Boolean
I'sProcedureHeader = True
Select Case True
Case B'eginsWith(UCase$("private sub"), UCase$(aLine))
Case B'eginsWith(UCase$("public sub"), UCase$(aLine))
Case B'eginsWith(UCase$("friend sub"), UCase$(aLine))
Case B'eginsWith(UCase$("sub"), UCase$(aLine))
Case B'eginsWith(UCase$("private function"), UCase$(aLine))
Case B'eginsWith(UCase$("public function"), UCase$(aLine))
Case B'eginsWith(UCase$("friend function"), UCase$(aLine))
Case B'eginsWith(UCase$("function"), UCase$(aLine))
Case B'eginsWith(UCase$("private property"), UCase$(aLine))
Case B'eginsWith(UCase$("public property"), UCase$(aLine))
Case B'eginsWith(UCase$("friend property"), UCase$(aLine))
Case B'eginsWith(UCase$("property"), UCase$(aLine))
Case True
I'sProcedureHeader = False
End Select
End Function
Private Function B'eginsWith( aBeginning As String, aText As String ) As Boolean
B'eginsWith = (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