Select Case Statement in VB.Net with Examples

โšก Smart Summary

Select Case in VB.Net is a conditional statement that tests one expression against a list of values and runs the matching block, giving cleaner, more readable branching than long If Then ElseIf chains.

  • ๐Ÿ” Purpose: Test one expression against many values and run the matching Case.
  • ๐Ÿงฉ Syntax: Each branch begins with Case; the block runs to End Select.
  • ๐Ÿ”ข Multiple matches: One Case accepts comma lists, To ranges, and Is comparisons.
  • ๐Ÿงฉ Case Else: Case Else runs only when no other Case matches the value.
  • ๐Ÿ”ค Case sensitivity: ToLower and ToUpper normalize text so matching ignores letter casing.
  • ๐Ÿค– AI help: GitHub Copilot and IntelliCode complete Select Case blocks as you type.

Select Case Statement in VB.Net

What is Select Case in VB.Net?

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.

Syntax of Select Case Statement in VB.Net

The Select Case statement is declared in VB.Net using 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 in the syntax:

  • 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.

VB.Net Select Case Statement Program Examples

Let us use an example to demonstrate how to use the Select Case statement in VB.Net:

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:

VB.Net Select Case program output printing Hello Guru99

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

VB.Net Select Case example code shown in Visual Studio

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.

Example 2

You can also let the user type the name, then make your decision based on that input.

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:

VB.Net Select Case console prompt asking to enter a name

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

VB.Net Select Case output after the user enters Guru99

Here is a screenshot of the code:

VB.Net Select Case code reading input with Console.ReadLine

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.

ToLower() and ToUpper() Functions in VB.Net

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.

Example of ToLower() and ToUpper() in VB.Net

Here is an example of the ToLower() and ToUpper() functions in VB.Net:

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:

VB.Net ToLower function console output for Select Case

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

VB.Net Select Case with ToLower matching uppercase Guru99 input

Here is a screenshot of the code:

VB.Net ToLower and ToUpper Select Case example 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 immediately be converted to john before the evaluation of the Case statements is done.

Rest of the code same as above.

FAQs

List values with commas (Case 6, 7, 8), give a range with Case 1 To 10, or use Case Is > 5.

Select Case tests one expression against many values and reads cleanly. Use If Then ElseIf for unrelated or complex Boolean conditions.

No. Only the first matching Case runs, then control skips past End Select. VB.Net needs no Break statement to prevent fall-through.

Case Else is the default branch, running only when no other Case matches. It is optional but safely handles unexpected values.

The expression must be an elementary data type such as Boolean, Char, Date, Integer, Long, Double, Decimal, Object, or String.

Yes. Place a complete Select Case inside a Case block of an outer one. The inner test runs only after the outer matches, though deep nesting hurts readability.

Yes. GitHub Copilot turns a plain-English comment into a complete Select Case block. Review that the cases cover every expected value.

Yes. Visual Studio 2026 pairs Copilot with IntelliCode for whole-line, context-aware Case completions.

Summarize this post with: