Selecting with Select CaseThe 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
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.)
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.
|
|
![]() |
![]() |
|||