Selecting with Select Case

The Select Case statement is more suited to checking for multiple conditions. Having more than three or four embedded If...Else statements results in a complicated program. You get into messy logic such as "If this is true, then if this is true, then if one more thing is true, then do something, else..." Here is the Select Case statement's format:

Select Case Expression
  Case expressionMatch  
     Block of one or more Visual Basic statements
  [ Case expressionMatch1
     Block of one or more Visual Basic statement]
  [ Case expressionMatch2
     Block of one or more Visual Basic statements]
      :
  [ Case expressionMatchN
     Block of one or more Visual Basic statements]
  [Case Else
     Block of one or more Visual Basic statements]
End Select
 

Select Case selects from one of several conditions. The number of conditions, indicated by the [ Case expressionMatch# ...] body, varies depending on the number of conditions that you need to test. If none of the cases perform a match, the Case Else code body executes if you supply one.

Despite its foreboding format, Select Case is simple to use. Consider the example in Listing 6.6.

Listing 6.6. Select Case statements compare against multiple values.

1:  ' Test for a child's letter grade
2:  Select Case txtGrade.Text
3:    Case "A"
4:       lblAnnounce.Caption = "Perfect!"
5:    Case "B"
6:       lblAnnounce.Caption = "Great!"
7:    Case "C"
8:       lblAnnounce.Caption = "Study harder!"
9:    Case "D"
10:      lblAnnounce.Caption = "Get help!"
11:   Case "F"
12:      lblAnnounce.Caption = "Back to basics!"
13:   Case Else
14:      lblAnnounce.Caption = "Error in grade"
15: End Select
 

The data type of the Expression must be the same as for each case's ExpressionMatch. Listing 6.6's code assumes that txtGrade.Text holds string letter grades; for that reason, lines 3, 5, 7, 9, and 11 all check to see whether that string value matches a string value.

If the text box named txtGrade.Text holds the letter A, line 3's Case body executes, and then Visual Basic skips all the remaining cases. Once that happens, the code that begins after line 13 executes. If the text box named txtGrade.Text holds the letter B, line 5's Case body executes, and so on. The body of a Case can cover several lines, although only single lines of code are shown in this example. Visual Basic knows that after a Case expressionMatch is made, each line in that matching Case body executes until the next Case, at which point the entire Select Case has done its job and the program can continue.

If, for some reason, a grade other than A, B, C, D, or F appears in the text box, the Case Else takes over and warns of the error by setting the label's value.

Visual Basic supports another form of Select Case that lets you specify one conditional operator for each expressionMatch using the Is keyword. Listing 6.7 rewrites the preceding Select Case to take advantage of conditional Select Case choices.

Listing 6.7. You can use conditional Select Case comparisons.

1:  ' Test for a child's numeric grade
2:  Select Case txtGrade.Text
3:    Case Is >= 90
4:       lblAnnounce.Caption = "Perfect!"
5:    Case Is >= 80
6:       lblAnnounce.Caption = "Great!"
7:    Case Is >= 70
8:       lblAnnounce.Caption = "Study harder!"
9:    Case Is >= 60
10:      lblAnnounce.Caption = "Get help!"
11:   Case Else
12:      lblAnnounce.Caption = "Back to basics!"
13: End Select

Given this format and the numeric grades, each case is dependent on the numeric grade being 90 or above for the best message and below 60 for the worst message. Notice that no test has to be made for a numeric grade less than 60 because if the grade is not between 60 and 100, the grade has to be below 60. (This example assumes that the grade will fall between 0 and 100 and not be bad data to illustrate how the Case Else can work as one of the Case statement bodies.)

Select Case statements don't work for all nested comparisons. No Select Case format supports the inclusion of logical operators, so you cannot use And, Or, Xor, or Not for the Select Case's test expression. You have to resort to a nested If...ElseIf...End If statement for complex nested conditions.

One final format of Select Case appears in the Visual Basic language; that Select Case allows for a range of choices using the To keyword. The range determines which Case body executes. Use the range-based Select Case when you can order the possibilities sequentially as shown in Listing 6.8.

Listing 6.8. Use a range for Select Case when comparing from grouped values.

1:  ' Test for a child's numeric grade
2:  Select Case txtGrade.Text
3:    Case 0 To 59
4:       lblAnnounce.Caption = "Back to Basics"
5:    Case 60 To 69
6:       lblAnnounce.Caption = "Get help!"
7:    Case 70 To 79
8:       lblAnnounce.Caption = "Study harder!"
9:    Case 80 To 89
10:      lblAnnounce.Caption = "Great!"
11:   Case Else
12:      lblAnnounce.Caption = "Perfect!"
13: End Select

Notice that the order of Listing 6.8's cases is different due to the range format being used. The first Case test, in line 3, checks for the lowest possible range. If the numeric grade falls between 0 and 59, the worst message appears in the label. (Integers are assumed, which could produce errors if someone enters 79.5 for a grade; but integers keep things simpler here.) Each succeeding range moves up sequentially. You can also test for string ranges as long as the lowest strings, conditionally according to the ASCII table, are tested earliest.

You can combine the various forms of Case expressions into a single Select Case statement. Here's a Case that uses all the formats to check for a value:

Case 101, 102, 201 To 205, Is > 300

If the expression in the Select Case statement is 101, 102, 201, 202, 203, 204, 205, or more than 300, the body of this Case executes.

Top Home