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:
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:
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:
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.
In this example, any number greater than 9 would result in a match.
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.
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
On the File menu, choose New Project.
In the New Project dialog box, in the Templates pane, click Windows Application.
In the Name box, type SelectCase and then click OK.
A new Windows Forms project opens.
From the Toolbox, drag one TextBox control and one Button control onto the form.
Double-click the button to open the Code Editor.
In the Button1_Click event handler, type the following code.
Press F5 to run the program.
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.
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."