Closer Look: Using Select Case to Decide Between Multiple Choices

In this lesson, you will learn to use the Select Case statement to run code based on multiple conditions.

The previous lesson demonstrated how to use If...Then statements to run different blocks of code for different conditions. While it's possible to evaluate more than two conditions in an If...Then statement by using the ElseIf keyword, the Select Case statement provides a much better way to evaluate multiple conditions.

The Select Case statement allows you to use as many conditions (or cases) as you need, making it convenient to write code for situations in which there are many choices. For example, suppose your program used a String variable to store a color choice and you needed to get the color value. The code for the Select Case statement might look like the following:

Select Case Color
  Case "red"
      MsgBox("You selected red")
  Case "blue"
      MsgBox("You selected blue")
  Case "green"
      MsgBox("You selected green")
End Select

When this code is run, the Select Case line determines the value (Color) of the expression. Assume that Color is a String variable and that this variable is a parameter for a method that contains the Select Case statement. The value of Color is then compared to the value for first Case statement. If the value matches, the next line of code is run, and then the code skips to the End Select line; if the value doesn't match, then the next Case line is evaluated.

The Case statement can take many different forms—in the example above, it is a String. However, it can be any data type or expression.

You can evaluate a range of numbers by using the To keyword, as follows:

Case 1 To 10

In this example, any number between 1 and 10 would result in a match.

You can also evaluate multiple values in a single Case statement by separating them with commas, as follows:

Case "red", "white", "green"

In this example, any of the three values would result in a match.

You can also use comparison operators and the Is keyword to evaluate values, as follows.

Case Is > 9

In this example, any number greater than 9 would result in a match.

Case Else

The above example works when you know all possible conditions, but what happens if there is a condition you didn't account for? For example, if the value of Color was yellow, the code would simply evaluate the three cases without finding a match, and no message box would display.

The Case Else statement can be used to run code when no match is found, as in the following example.

Select Case Color
  Case "red"
      MsgBox("You selected red")
  Case "blue"
      MsgBox("You selected blue")
  Case "green"
      MsgBox("You selected green")
  Case Else
      MsgBox("Please choose red, blue, or green")
End Select

In the above code, if the value of Color is yellow, the code compares it with the first three Case lines without finding a match. When the Case Else line is reached, the next line of code is run before moving to End Select.

To use the Select Case statement

  1. On the File menu, choose New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type SelectCase and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag one TextBox control and one Button control onto the form.

  5. Double-click the button to open the Code Editor.

  6. In the Button1_Click event handler, type the following code.

    Dim Number As Integer = CInt(Textbox1.Text)
    Select Case Number
      Case 1
          MsgBox("Less than 2")
      Case 2 To 5
          MsgBox("Between 2 and 5")
      Case 6, 7, 8
          MsgBox("Between 6 and 8")
      Case 9 To 10
          MsgBox("Greater than 8")
      Case Else
          MsgBox("Not between 1 and 10")
    End Select
    
  7. Press F5 to run the program.

  8. In the text box, type a number and click the button.

    A message box appears displaying the message for the Case statement matching the number that you entered.

Next Steps

In this topic, you learned how to use the Select Case statement to choose between multiple conditions. At this point, you can go on to the next lesson, "What to Do When Something Goes Wrong: Handling Errors."

Next Lesson: What To Do When Something Goes Wrong: Handling Errors

See Also

Tasks

What To Do When Something Goes Wrong: Handling Errors

Making a Program Choose Between Two Possibilities: The If...Then Statement

Reference

Select...Case Statement (Visual Basic)