Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Two Additional Select Case Formats

The two additional formats differ only slightly from the standard Select Case that you learned about in the previous section. They enable you to extend the power of Select Case so that Visual Basic can make Case matches on both comparison tests and ranges of values. Here is the first additional format:

Select Case Expression
  Case Is Relation:
    One or more Visual Basic statements
  Case Is Relation:
    One or more Visual Basic statements
  [Case Is Relation:
     One or more Visual Basic statements]
  [Case Else:
     One or more Visual Basic statements]
End Select

Relation can be whatever comparison test you want to perform against Expression at the top of the Select Case. The standard Select Case statement, discussed in the previous section, compared the Expression value against an exact Case match. When you use the comparison Is Select Case option, each Case can be matched on a comparison test.

The format of the second extra Select Case format is as follows:

Select Case Expression
  Case expr1 To expr2:
    One or more Visual Basic statements
  Case expr1 To expr2:
    One or more Visual Basic statements
  [Case expr1 To expr2:
     One or more Visual Basic statements]
  [Case Else:
     One or more Visual Basic statements]
End Select

The Case lines require a range, such as 4 To 6. The To Select Case option enables you to match against a range instead of a relation or an exact match.

Study Listing 7.5 to learn how to combine different Select Case statements to test for various values.

Example 7.5. Using Select Case to simplify complex nested If...Else statements.

 1: Rem The following Select Case to End Select code
 2: Rem   assigns a student's grade and school name
 3: Rem   to the label on the form. The code checks
 4: Rem   to make sure that the student is not too
 5: Rem   young to be going to school.
 6: Select Case intAge
 7: ' Check for too young...
 8: Case Is <5:  lblTitle.Text = "Too young"
 9: 
10: ' Five-year olds are next assigned
11: Case 5: lblTitle.Text = "Kindergarten"
12: 
13: ' Six to eleven...
14: Case 6 To 11:  lblTitle.Text = "Elementary"
15: lblSchool.Text = "Lincoln"
16: 
17: ' Twelve to fifteen...
18: Case 12 To 15: lblTitle.Text = "Intermediate"
19: lblSchool.Text = "Washington"
20: 
21: ' Sixteen to eighteen
22: Case 16 To 18: lblTitle.Text = "High School"
23: lblSchool.Text = "Betsy Ross"
24: 
25: ' Everyone else must go to college
26: Case Else: lblTitle.Text = "College"
27: lblSchool.Text = "University"
28: End Select

If the age is less than 5, the title label becomes Too young, and the school name remains blank. If the age is exactly 5 (intAge is obviously an integer value), the title gets Kindergarten, and the school name still remains blank. Only if the child is 5 or older are both the title and school name initialized.

If you were to rewrite this code using embedded If...Else logic, the code would become a nightmare. The Select Case's range testing, such as Case 16 to 18, saves a tremendous amount of If...Else logic.

Share ThisShare This

Informit Network