logo  Select Case
This Chapter
If
Select
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
The Select Case ... End Select block provides an alternative to multiple If statements. "Select Case" is ideal for situations where the value of a single variable can result in many different actions being required. In this first example, we will replace the first set of If statements with a single Select Case block

If i = 1 Then
 Return "One"
End If
If i = 2 Then
 Return "Two"
End If
If i = 3 Then
 Return "Three"
End If
If i = 4 Then
 Return "Four"
End If
If i = 5 Then
 Return "Five"
End If
'
Select Case i
 Case 1
  Return "One"
 Case 2
  Return "Two"
 Case 3
  Return "Three"
 Case 4
  Return "Four"
 Case 5
  Return "Five"
End Select
  
Select Case also supports "Case Else" this keyword must be the last "Case" before the "End Select". Here is an example of some If statements and the equivilent Select Case

If i = 1 Then
 Return "One"
End If
If i = 2 Then
 Return "Two"
End If
If i <> 1 And i <> 2 Then
 Return "Not One and Not Two"
End If
'
Select Case i
 Case 1
  Return "One"
 Case 2
  Return "Two"
 Case Else
  Return "Not One and Not Two"
End Select
  
A single Case within the select can be triggered by more than one condition. Separate the conditions using a comma

If i = 1 or i = 2 Then
 Return "One or Two"
End If
If i <> 1 And i <> 2 Then
 Return "Not One and Not Two"
End If
'
Select Case i
 Case 1, 2
  Return "One or Two"
 Case Else
  Return "Not One and Not Two"
End Select
  
Select Case will also accept operators e.g. < <= = >= > ≤>

If i > 2 Then
 Return "Greater than 2"
ElseIf i <= 4 Then
 Return "Less than or equal to 4" 
End If
'
Select Case i
 Case > 2
  Return "Greater than 2"
 Case <= 4
  Return "Less than or equal to 4"
End Select
  
You can also use the keyword "To" within a Case to denote anything in a Range is valid

If i >= 2 And i <= 4 Then
 Return "In the range 2 to 4"
End If
'
Select Case i
 Case 2 To 4
  Return "In the range 2 to 4"
End Select
  
You can freely mix fixed values, operators and ranges. The "Case" can work with any type of variable. The following function is valid VB.Net code. The Case keyword is "case-sensitive", this means that a test for the string "CAT" would not accept "cat".

Private Function TestSelect(myWord as String) As Integer
Dim ret As String = String.Empty
'
Select Case myWord
 Case "the" : ret = 1
 Case "dog", "cat" : ret = 2
 Case >= "A", <= "Z" : ret = 3
 Case "Tom", > "Dick", "Harry" To "Holly" : ret = 4
 Case Else : ret = 5
End Select
'
Return Ret
End Function
  

 
Important Notes
Many "C" style computer languages (ones with code full of ";" "{" and "}") have Select Case equivilents that are subtly different to VB.Net. VB.Net will automatically jump out of the "Select Case" "End Select" after running all code within a successfull Case match. C based languages are likely to check all other Cases They could run the code linked to more than one Case. Here is an example to expain the difference

' VB.NET Version
Dim ret As String = String.Empty
Dim i as Integer = 2
Select Case i
 Case < 0 : ret &= "(0)"
 Case < 10 : ret &= "(10)"
 Case < 20 : ret &= "(20)"
 Case Else : ret &= "(Big number)"
End Select
The VB.Net version ignores "Case < 0" because 2 is not less than 0
It then accepts "Case < 10" because 2 is less than 0.
The variable ret changes from "" to "(10)"
VB.Net has now completed a Case
It immediately jumps out of the Select ... End Select block.
VB.Net will not run Case Else because it has already run another Case
The VB.Net code exits with ret = "(10)"
 
We will now look at a similar function written in a "C" style language

// Similar code in a generic C style language
String ret = "";
Int i = 2;
Select Case i{
 Case < 0 { ret &= "(0)" };
 Case < 10 { ret &= "(10)" };
 Case < 20 { ret &= "(20)" };
 Case Else { ret &= "(Big number)" };
}
The C-Style version ignores "Case < 0" because 2 is not less than 0
It then accepts "Case < 10" because 2 is less than 10.
The variable ret changes from "" to "(10)"
Unlike VB.Net, the C-Style compiler does not exit the Select Case
It also accepts "Case < 20" because 2 is less than 20.
The variable ret changes from "(10)" to "(10)(20)"
C-style not run Case Else because it has already run another Case
The C-Style code exits with ret = "(10)(20)"
 

DigitalDan.co.uk