Select...Case Statement in VB.Net with Example

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:

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 = "gtupapers"

        Select Case name

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

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

            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 gtupapers 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 gtupapers.
  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 "gtupapers"
                Console.WriteLine("Hello gtupapers")

            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 gtupapers 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 gtupapers.
  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 gtupapers as different from gtupapers. 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 "gtupapers." 
                Console.WriteLine("Hello gtupapers")

            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 gtupapers (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 gtupapers, it will be immediately converted to gtupapers, 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

 

YOU MIGHT LIKE: