Multiple Choice with Select Case

If is great for data comparisons in cases where one or two comparison tests must be made. When you must test against more than two conditions, however, If becomes difficult to maintain. The logical operators help in only certain kinds of conditions. At other times, you must nest several If...Else statements inside one another.

Consider the If statement shown in Listing 7.3. Although the logic of the If statement is simple, the coding is extremely difficult to follow.

Listing 7.3. Nested If...Else statements quickly become complex.

1: If (intAge = 5) Then

2: lblTitle.Caption = “Kindergarten”

3: Else

4: If (intAge = 6) Then

5: lblTitle.Caption = “1st Grade”

6: Else

7: If (intAge = 7) Then

8: lblTitle.Caption = “2nd Grade”

9: Else

10: If (intAge = 8) Then

11: lblTitle.Caption = “3rd Grade”

12: Else

13: If (intAge = 9) Then

14: lblTitle.Caption = “4th Grade”

15: Else

16: If (intAge = 10) Then

17: lblTitle.Caption = “5th Grade”

18: Else

19: If (intAge = 11) Then

20: lblTitle.Caption = “6th Grade”

21: Else

22: lblTitle.Caption = “Advanced”

23: End If

24: End If

25: End If

26: End If

27: End If

28: End If

29: End If

Visual Basic supports a Select Case statement that handles such multiple-choice conditions better than If...Else. Here is the format of the Select Case statement:

Select Case Expression

Case value

One or more Visual Basic statements

Case value

One or more Visual Basic statements

[Case value

One or more Visual Basic statements]

[Case Else

One or more Visual Basic statements]

End Select

Select Case is a good substitute for long, nested If...Else conditions when several choices are possible. You set up your Visual Basic program to execute one set of Visual Basic statements from a list of statements inside Select Case.

The Select Case format makes the statement look as difficult as a complex nested If...Else, but you will soon see that Select Case statements are actually easier to code and maintain than their If...Else counterparts.

Expression can be any Visual Basic expression[md]such as a calculation, a string value, or a numeric value[md]provided that it results in an integer or a string value. Each value must be an integer or a string value that matches Expression’s datatype.

The Select Case statement is useful when you must make several choices based on data values. Select Case can have two or more Case value sections. The code that executes depends on which value matches Expression. If none of the values match Expression, the Case Else body of code executes if you code the Case Else. Otherwise, nothing happens and control continues with the statement that follows End Select.

caution

Don’t use Select Case when a simple If or If...Else will suffice. Test logic is often so straightforward that a Select Case would be overkill and even less clear than an If. Unless you need to compare against more than a couple values, stick with the If and If...Else statements because of their simplicity.

The fastest way to learn Select Case is to see an example of it. Listing 7.4 contains a Select Case version of the child grade assignments shown in Listing 7.3. Select Case organizes the multiple-choice selections into a more manageable format.

Listing 7.4. Using Select Case to simplify complex nested If...Else statements.

1: Select Case intAge

2: Case 5: lblTitle.Caption = “Kindergarten”

3: Case 6: lblTitle.Caption = “1st Grade”

4: Case 7: lblTitle.Caption = “2nd Grade”

5: Case 8: lblTitle.Caption = “3rd Grade”

6: Case 9: lblTitle.Caption = “4th Grade”

7: Case 10: lblTitle.Caption = “5th Grade”

8: Case 11: lblTitle.Caption = “6th Grade”

9: Case Else: lblTitle.Caption = “Advanced”

10: End Select

tip

Use Select Case instead of embedded If...Else because Select Case keeps the code much simpler and easier to maintain.

Here’s how the Select Case works: If the intAge variable holds the value 5, the label is assigned “Kindergarten” in the second line. If the intAge variable holds the value 6, the label is assigned “1st Grade” in the third line. The logic continues through the Case 11: statement. If intAge holds a value that doesn’t fall within the range of 5 through 11, the final Case Else assigns “Advanced” to the label.

The body of each Case can consist of more than one statement, just as the body of an If or If...Else can consist of more than one statement. Visual Basic executes all the statements for any given Case match until the next Case is reached. When Visual Basic executes a matching Case value, it skips the remaining Case statements and continues with the code that follows the End Select statement.

Notice the colons after each Case value statement in Listing 7.4. The colons are optional, but they do help separate the case being tested from its code that executes.

note

Programmers often trigger the execution of complete procedures, such as event procedures, from within a Case statement. Instead of putting several statements in the body of an If...Else or a Case, you can execute a procedure that contains all the statements that execute when a given condition is true.

Top Home