Visual Basic Select Case
If you have programmed before in C# or PHP you might be familiar with the Switch Statement, which is called the Select Case in VB. Select Case statements are just like If Else Statements but have some differences: they only evaluate one thing and are more readable.
Syntax
Select Case VariableName Case 1 Case 2 Case Else End Select
As an example, first create a VB Console Application and name it Select Case, then copy the following:
Dim game As String Console.WriteLine("Your favourite game?") game = Console.ReadLine() Dim message As String message = game & " is your favourite game!" Select Case game Case "GTA 4" Console.WriteLine(message) Case "Battlefield 3" Console.WriteLine(message) Case "GRID" Console.WriteLine(message) Case "Infamous" Console.WriteLine(message) Case Else Console.WriteLine("Looks like your game is not on my list") End Select Console.ReadLine()
In the above example we collect the user’s input and simply compare it through the case labels. If the information matches any labels, the code for that case label will execute; otherwise the Case Else will execute.
Case Sensitive
The issue with the above example is that the case labels are case sensitive, so if the user typed “gta 4” as their favourite game, the Case Else will execute as “gta 4”. This is not in our case label, as it actually appears all upper-case, so we can solve this issue by using the ToLower() method.
Dim game As String Console.WriteLine("Your favourite game?") game = Console.ReadLine() Dim message As String message = game & " is your favourite game!" Select Case game.ToLower() Case "gta 4" Console.WriteLine(message) Case "battlefield 3" Console.WriteLine(message) Case "grid" Console.WriteLine(message) Case "infamous" Console.WriteLine(message) Case Else Console.WriteLine("Looks like your game is not on my list") End Select Console.ReadLine()
Here we have added the ToLower() method to our variable name, and we changed the case labels all to lower-case. This is because when the case label compares it, it essentially compares “gta 4” to “gta 4” the user’s input will all be lower-case, so even if the user types “gTa 4” it will be converted to lower-case. Therefore, the case labels need to be lower-case so they can be compared.
The Case Else exists because if the other cases do not match, then that message appears.
Select Case – Using Operators
We can use the following operators in your case labels:
Operator | Description |
---|---|
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
<> | Not equal to |
= | Equal to |
When using operators you need to use the Is or To keyword, so the case label would look like this:
Case Is > 10
Case 13 To 20
The case label translates to “Is Greater than 10?” and the second translates to “Is between 13 and 20”. We can only use the relational operators if we are evaluating an integer or double.
Example
Dim age As Integer
Console.Write("Please enter your age")
age = Console.ReadLine()
Select Case age
Case Is < 17
Console.WriteLine("You are not old enough to drink alcohol")
Case Is >= 18
Console.WriteLine("You are old enough to drink alcohol, but drink carefully!")
Case Else
Console.WriteLine("Didn't understand that")
End Select
Console.ReadLine()
In this example you can see the use of the greater than and greater than or equal to operators to match the user’s input. The basic example takes the input from the user and then compares it to the case labels.
Example – Using To Keyword
Dim age As Integer Console.Write("Please enter your age") age = Console.ReadLine() Select Case age Case 10 To 17 Console.WriteLine("You are not old enough to drink alcohol") Case 18 To 50 Console.WriteLine("You are old enough to drink alcohol, but drink carefully!") Case Else Console.WriteLine("Didn't understand that") End Select Console.ReadLine()
This example is similar to the previous one, however this time we are using the To keyword and comparing between a range of numbers.
Example – Strings
In the above examples we used integers; this time we will use strings and use the <> operator.
Dim car As String Console.WriteLine("Enter your favourite car") car = Console.ReadLine() Select Case car.ToLower() Case Is <> "audi" Console.WriteLine("It's not an audi!") Case Else Console.WriteLine("It was an Audi!") End Select Console.ReadLine()
Similar to the above statements, this time we use a string.
Summary
This tutorial addressed using the Select Case in Visual Basic, and you learned about the operators you can use, as well as dealing with case sensitivity issues.