Visual Studio 2013 Lesson : Case Select

[Lesson 47] <<[Contents] 

Case select

Select Case. In branches, in selection statements, programs change course. Select Case rapidly matches values. In it, we specify a set of constants (Integers, Chars, Strings).
Matching. Select Case evaluates an expression and goes to the matching clause. Cases do not fall through, and no "exit" statement is required. But we can stack cases that share statements.
First example. To create a Select Case statement, type Select and press tab. Then, edit the variable name. We read a line from the Console, call Integer.Parse on it, and then use Select.

Else:Case Else is the default case. When no other values match, this case is reached.

Based on: .NET 4.5

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

Nested. Sometimes a nested Select Case statement is useful. For example, we can test characters in a String, one after another, with nested Selects.

And:We can use this style of logic to optimize StartsWith or EndsWith calls. This is only needed on performance-critical code.

StartsWith, EndsWith

Chars:The example uses chars within a String as the Select Case expression. This is a common construct.

Char
VB.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

Strings. Select Case may be used on a String. With this statement we match a variable against a set of values such as String literals.

But:On Strings, Select Case offers no performance advantage as with Integers (or other values).

Strings

To 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 "perls", 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 "perls"
		    Console.WriteLine("Word 3")
		Case Else
		    Console.WriteLine("Something else")
	    End Select
	End While
    End Sub
End Module

Output

dot
Word 1
perls
Word 3
test
Something else

Internals. On Strings, does Select Case ever compile into anything other than a series of comparisons? I changed the above program to have nine String literals.

Result:No Dictionary was used by the compiler. VB.NET lacks the String Dictionary optimization for C# String Switch constructs.

Therefore:If you have to match a lot of string literals, building a Dictionary might be faster.

Dictionary
Variables. VB.NET allows variable cases. But we may lose optimizations with this syntax. Each case must be evaluated and cannot be stored in a lookup table.

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.

Integer

Note 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

Stacked cases. Multiple cases can be combined by specifying them one after another. In this example, both 99 and 100 will reach the same Console.WriteLine statement.

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

[Lesson 47] <<[Contents]