C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Else: Case Else is the default case. When no other values match, this case is reached.
VB.NET program that uses Select Case
Module Module1
Sub Main()
Dim value As Integer = Integer.Parse(Console.ReadLine())
Select Case value
Case 1
Console.WriteLine("You typed one")
Case 2
Console.WriteLine("You typed two")
Case 5
Console.WriteLine("You typed five")
Case Else
Console.WriteLine("You typed something else")
End Select
End Sub
End Module
Output
2
You typed two
And: We can use this style of logic to optimize StartsWith or EndsWith calls. This is only needed on performance-critical code.
StartsWith, EndsWithChars: The example uses chars within a String as the Select Case expression. This is a common construct.
CharVB.NET program that uses nested Select Case
Module Module1
Sub Main()
Dim value As String = "cat"
' Handle first letter.
Select Case value(0)
Case "c"
' Handle second letter.
Select Case value(1)
Case "a"
Console.WriteLine("String starts with c, a")
Case "o"
' Not reached:
Console.WriteLine("String starts with c, o")
End Select
End Select
End Sub
End Module
Output
String starts with c, a
But: On Strings, Select Case offers no performance advantage as with Integers (or other values).
StringsTo start: Let's evaluate a program that reads an input from the Console. Then it uses the Select Case statement on that value.
Values: It matches against four possible values: "dot", "net", and "Codex", and also all other values (Else).
VB.NET program that uses Select Case on String
Module Module1
Sub Main()
While True
Dim value As String = Console.ReadLine()
Select Case value
Case "dot"
Console.WriteLine("Word 1")
Case "net"
Console.WriteLine("Word 2")
Case "Codex"
Console.WriteLine("Word 3")
Case Else
Console.WriteLine("Something else")
End Select
End While
End Sub
End Module
Output
dot
Word 1
Codex
Word 3
test
Something else
Note: The "value" Integer is set to 10. And we match it against the variable y, which also equals 10, in a Select Case statement.
IntegerNote 2: An advanced compiler could analyze this program before execution so that no branches are evaluated at runtime.
VB.NET program that uses variable Cases
Module Module1
Sub Main()
Dim value As Integer = 10
Dim x As Integer = 5
Dim y As Integer = 10
' Select with cases that are variables.
Select Case value
Case x
' Not reached.
Console.WriteLine("Value equals x")
Case y
Console.WriteLine("Value equals y")
End Select
End Sub
End Module
Output
Value equals y
Important: Stacked cases must be specified on the same line. Separate "case" statements mean empty blocks of code, not a group of cases.
VB.NET program that uses stacked cases
Module Module1
Sub Main()
Dim value As Integer = Integer.Parse("99")
Select Case value
Case 99, 100
' Both 99 and 100 will end up here.
Console.WriteLine("99 or 100")
Case 101
Console.WriteLine("Not reached")
End Select
End Sub
End Module
Output
99:
99 or 100
100:
99 or 100
IsValidNumber: This is a Boolean Function that returns True if the argument integer is valid, and False for all other numbers.
Case 100, 200: IsValidNumber considers the numbers 100 and 200 to be valid, but all other integers are not valid (according to its logic).
VB.NET program that returns from Select Case
Module Module1
Function IsValidNumber(ByVal number As Integer) As Boolean
' Return from a Select Case statement in a Function.
Select Case number
Case 100, 200
Return True
End Select
' Other numbers are False.
Return False
End Function
Sub Main()
Console.WriteLine("ISVALIDNUMBER: {0}", IsValidNumber(100))
Console.WriteLine("ISVALIDNUMBER: {0}", IsValidNumber(200))
Console.WriteLine("ISVALIDNUMBER: {0}", IsValidNumber(300))
End Sub
End Module
Output
ISVALIDNUMBER: True
ISVALIDNUMBER: True
ISVALIDNUMBER: False
Note: Duplicate cases could mean an error in a program. It might be best to order cases from low to high to easily spot this issue.
VB.NET program that has duplicate case statements
Module Module1
Sub Main()
Dim value As Integer = 1
Select Case value
Case 1
Console.WriteLine("FIRST CASE REACHED")
Case 1
Console.WriteLine("SECOND CASE")
End Select
End Sub
End Module
Output
FIRST CASE REACHED
Version 1: In this version of the code, we use an If-statement, along with ElseIf to test values.
If ThenVersion 2: Here we use the Select Case statement, along with Cases, to test for possible values.
Result: The Select Case statement is faster. The results are the same for the constructs.
Info: Try changing the value to 0, not 2. The If-statement will perform faster, because the first check matches the value each time.
VB.NET program that times If Then, Select Case
Module Module1
Sub Main()
Dim m As Integer = 300000000
Dim value As Integer = 2
' Version 1: Use If-Statement.
Dim total As Integer = 0
Dim s1 As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To m - 1
If value = 0 Then
total -= 1
ElseIf value = 1 Then
total -= 100
ElseIf value = 2 Then
total += 1
End If
Next
s1.Stop()
' Version 2: Use Select Case.
total = 0
Dim s2 As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To m - 1
Select Case value
Case 0
total -= 1
Case 1
total -= 100
Case 2
total += 1
End Select
Next
s2.Stop()
Console.WriteLine((s1.Elapsed.TotalMilliseconds /
1000).ToString("0.00 s"))
Console.WriteLine((s2.Elapsed.TotalMilliseconds /
1000).ToString("0.00 s"))
End Sub
End Module
Output
1.47 s: If Then, value = 2
0.86 s: Select Case, value = 2
Note: With this opcode, Select Case is faster (on certain programs and data) than an If-statement.
And: If we have 1,000 cases, but one is most common, testing for the common one with an If-statement is fastest.
Thus: VB.NET seems to lack the String Dictionary optimization for C# string switch constructs.
And: If you have to match many string literals, building a Dictionary (and using TryGetValue might be faster.
DictionaryOften: It is the best approach to use whatever syntax form (if or switch) is clearest.
Credit: Thanks to Leen Boers for helping correct an error in one of the Select Case examples.