What is a Select Case?

Select Case is a conditional statement, that helps you test a variable for equality against a set of values. Each value is referred to as a case, and a variable that is being switched on should be checked for all the select cases.

The Select Case statement provides you with an easy way of testing for the contents of a variable. However, it is only suitable for use when a variable in question has only a limited number of options.

In this VB Net tutorial, you will learn:

Syntax of Select Case

The Select Case statement takes the following syntax:

Select [ Case ] your_expression
   [ Case expression_list
      [ statement(s) ] ]
   [ Case Else
      [ else statement(s) ] ]
End Select

Let us describe the parameters used above:

  • your_expression: this denotes an expression which evaluates to one of the elementary data types supported in Microsoft VB.NET.
  • expression_list: expression clauses that denote the match values for the expression. For the case of multiple clauses, separate them using a comma (,).
  • statement(s): statements that follow the Case and they execute after the select expression has matched any clause in expression_list.
  • else statements: statements that follow the Case Else and run once the select expression fails to match any of the clauses in the expression_list for any Case statement.

Examples

Let us use an example to demonstrate how to use this statement.

Step 1) First, create a new console application.

Step 2) Use the following code:

Module Module1
    Sub Main()

        Dim name As String

        name = "Guru99"

        Select Case name

            Case "John"
                Console.WriteLine("Hello John")

            Case "Guru99"
                Console.WriteLine("Hello Guru99")

            Case "Alice"
                Console.WriteLine("Hello Alice")

            Case "Joel"
                Console.WriteLine("Hello Joel")

            Case Else
                Console.WriteLine("unknown name")

        End Select
        Console.WriteLine("VB.NET is easy!")

        Console.ReadKey()

    End Sub

End Module

Step 3) Click the Start button from the top bar to execute the program. It will give you the following result:

Here is a screenshot of the code that we have used:

Explanation of Code:

  1. Creating a module named Module1.
  2. Start of the main sub-procedure.
  3. Creating a string variable named name.
  4. Assigning a value of Guru99 to the variable name.
  5. The value of a variable name will be used for performing comparisons with the various Case statements to find a match.
  6. If the value of a variable name is John.
  7. Text to print on the console if the above Case is true/matched.
  8. If the value of a variable name is Guru99.
  9. Text to print on the console if the above Case is true/matched.
  10. If the value of a variable name is Alice.
  11. Text to print on the console if the above Case is true/matched.
  12. If the value of a variable name is Joel.
  13. Text to print on the console if the above Case is true/matched.
  14. If none of the above Case statements is true/ is matched.
  15. Text to print on the console if the above Case is true, that is, no Case statement is matched.
  16. End of the Select statement.
  17. This statement will print some text on the screen regardless of whether a Case statement was matched or not. It will always execute.
  18. Pause the console window for a while waiting for a user to take action to close it.
  19. End of the main sub-procedure.
  20. End of the module.

You can also allow the user to type the name you make your decision based on that. For example:

Step 1) Create a new console application.

Step 2) Use the following code:

Module Module1
    Sub Main()

        Console.Write("Enter your name: ")

        Dim name As String = Console.ReadLine()

        Select Case name

            Case "John"
                Console.WriteLine("Hello John")

            Case "Guru99"
                Console.WriteLine("Hello Guru99")

            Case "Alice"
                Console.WriteLine("Hello Alice")

            Case "Joel"
                Console.WriteLine("Hello Joel")

            Case Else
                Console.WriteLine("unknown name")

        End Select
        Console.WriteLine("VB.NET is easy!")

        Console.ReadKey()

    End Sub

End Module

Step 3) Click the Start button from the top bar to execute it. It should return the following:

Step 4) Type the name Guru99 and press the enter key. You should get the following:

Here is a screenshot of the code:

Explanation of Code:

  1. Creating a module named Module1.
  2. Start of the main sub-procedure.
  3. Printing some text on the console instructing the user to enter their name.
  4. Creating a string variable named name and prompting the user to enter a value for this variable on the console.
  5. The value of the variable name will be used for performing comparisons with the various Case statements to find a match.
  6. If the value of the variable name is John.
  7. Text to print on the console if the above Case is true/matched.
  8. If the value of the variable name is Guru99.
  9. Text to print on the console if the above Case is true/matched.
  10. If the value of the variable name is Alice.
  11. Text to print on the console if the above Case is true/matched.
  12. If the value of the variable name is Joel.
  13. Text to print on the console if the above Case is true/matched.
  14. If none of the above Case statements is true/ is matched.
  15. Text to print on the console if the above Case is true, that is, no Case statement is matched.
  16. End of the Select statement.
  17. This statement will print some text on the screen regardless of whether a Case statement was matched or not. It will always execute.
  18. Pause the console window for a while waiting for a user to take action to close it.
  19. End of the main sub-procedure.
  20. End of the module.

Case Sensitive: ToLower, ToUpper

The Select Case statement is case sensitive. This means that it will treat guru99 as different from Guru99. However, we can use the ToLower() and the ToUpper() functions to handle the issue of a case with this statement.

For example:

Step 1) Create a new console application.

Step 2) Use the following code:

Module Module1
    Sub Main()

        Console.Write("Enter your name: ")

        Dim name As String = Console.ReadLine()

        Select Case name.ToLower()

            Case "john." 
                Console.WriteLine("Hello John")

            Case "guru99." 
                Console.WriteLine("Hello Guru99")

            Case "alice." 
                Console.WriteLine("Hello Alice")

            Case "joel." 
                Console.WriteLine("Hello Joel")

            Case Else
                Console.WriteLine("unknown name")

        End Select
        Console.WriteLine("VB.NET is easy!")

        Console.ReadKey()

    End Sub

End Module

Step 3) Click the Start button on the top bar to run the code. You should get the following output:

Step 4) Type the name Guru99 (G is uppercase) and press the enter key on your keyboard. You should get the following:

Here is a screenshot of the code:

Explanation of Code:

Code line 5: The value of the variable name will be used for performing comparisons with the various Case statements to find a match. The ToLower() function will ensure that any name the user types is first converted into lowercase before the evaluation of Case statements. This means that if the user types Guru99, it will be immediately converted to guru99, then the evaluation of the Case statements is done. If the user types John, it will be immediately be converted to john before the evaluation of the Case statements is done.

Rest of the code same as above.

Summary

  • The Select Case statement provided by VB.NET helps you evaluate a value against a set of values through matching.
  • It is only suitable if the possible values of the variable in question is known to be limited.
  • The Select Case statement is case sensitive.
  • You can use the ToLower() and the ToUpper() functions to convert strings into lowercase and uppercase respectively.

 

YOU MIGHT LIKE: