The Select Case Statement

The Select Case Statement

Chapter 9, "Arithmetic and Assignment Operators," provides a brief look at the Select Case statement. This section provides additional details on how to use the Select Case statement. This is the syntax format for the Select Case statement:

 Select  Expression1  Case 1     ' Statement block for Case 1   Case 2     ' Statement block for Case 2   Case N     ' More Case blocks as needed   Case Else     ' Catch-all End Select 

Expression1 in the Select statement is evaluated. If the expression matches a Case value, that Case 's statement block is executed. When all the statements in that Case 's statement block are executed, program control is sent to the statement that follows the End Select statement. If Expression1 does not match any Case value, the Case Else statement block is executed. The Case Else statement is optional, so it is possible for no Case statements to be executed if Expression1 does not match any Case value.

For example, suppose MyDay can take the values 1 through 7, where 1 is Monday, 2 is Tuesday, and so on. Further assume that certain tasks are to be performed on each day of the week. The Select Case block code to do this might look like that shown in Listing 11.3.

Listing 11.3 A Select Case Block for Days of the Week
 Select MyDay   Case 1    ' Monday     TakeHeadacheMedicine()   Case 2    ' Tuesday     GroceryShop()   Case 3    ' Wednesday     PayBills()   Case 4    ' Thursday     PayMoreBills()   Case 5    ' Friday     LeagueNight()   Case 6    ' Saturday     Bathe()   Case 7    ' Sunday     Rest()   Case Else  ' Catch-all     MessageBox.Show("Improper value for MyDay") End Select 

For any given value of MyDay , the associated Case statement block is executed. As with other statement blocks, each Case statement block can have more than one statement if needed.

Notice how Listing 11.3 uses the Case Else statement to trap any unexpected values for MyDay that might creep into the program. This is a fairly common practice and is an example of defensive coding. Planning for the unexpected is almost always a good thing.

Select Case Statement Variations

Some very useful variations make the Select Case statement especially easy to use. For example, suppose you want to set a variable to the number of days in the month. You could use 12 Case statements, but you don't need to. After all, most of the months have either 30 or 31 days. Assuming that 1 equals January, 2 equals February, and so on, consider the following code fragment:

 Select MyMonth    Case 2    ' February     Days = 28 + LeapYear()   Case 4, 6, 9, 11     Days = 30   Case Else     Days = 31 End Select 

Notice the middle Case statement. You can use a single Case statement that resolves to multiple values by simply separating the values with commas. In the code example, if MyMonth equals 4 , 6 , 9 , or 11 , the statement block that assigns 30 to Days is executed. Therefore, Days equal 30 for April ( 4 ), June ( 6 ), September ( 9 ), and November ( 11 ).

What do you suppose the Case 2 statement does? It calls a function named LeapYear() and adds whatever it returns to 28 and then assigns it to Days . Visual Basic .NET does not provide a function named LeapYear() , but if it did, the function would probably return 1 for a leap year and otherwise . If that is the way LeapYear() worked, Days would equal 29 for leap years and 28 otherwise.

Select Case Statements with Ranges of Values

You might have a situation in which you want to test for several values, but there is a range of values that should cause a single statement to be executed. Here's an example to help explain this:

 Select Number    Case 1, 2     Answer = 4   Case 3 To 6    ' A range of values     Answer = 6   Case 11, 13 To 15     Answer = 8   Case Else     Answer = 0 End Select 

In the second Case statement, Answer is set to 6 if Number equals 3 , 4 , 5 , or 6 . The To keyword allows you to specify a range of values for a single Case statement. Notice that the range is inclusive. In the next Case statement, Answer is set to 8 if Number is 11 , 13 , 14 , or 15 . In other words, you can have a comma-separated list of numbers and a range that uses the To keyword in the same Case statement.

Select Case Statements with Limit Ranges

At some point you might want to test several different ranges with a minimum of fuss. Consider the following code fragment:

 Select Case Number   Case 1   txtResult.Text = "1"  Case 2 To 10   txtResult.Text = "2"  Case Is < 20   txtResult.Text = "3"  Case Is > 20   txtResult.Text = "4" End Select 

Notice how the first two Case statements cover the values between 1 and 10 for Number . At first glance, the third Case statement seems to overlap the first two Case statements, but that is not how the Is keyword works in a Case statement. The Is keyword is followed by a relational operator and then an operand. The relational test is performed with respect to all the previous Case values and the operand. In this code fragment, txtResult displays 3 for any value of Number between 11 and 19. This occurs because the prior Case values include the values through 10. The last Case statement would be executed for any value of Number that is greater than 20. Note that the value 20 is not covered by any of the Case statements.

You have to be a little careful when you use the Is keyword in a Case statement. The next code fragment is the same as the preceding one, except that the first Case is moved to the bottom of the Select Case block:

 Select Case Number   Case 2 To 10   txtResult.Text = "2"  Case Is < 20   txtResult.Text = "3"  Case Is > 20   txtResult.Text = "4"  Case 1   txtResult.Text = "1" End Select 

If you type this code into your program and enter 1 for Number , txtResult displays 3 , not 1 . This happens because Select evaluates Number (that is, Expression1 ) and then starts looking at each Case value for a match. Visual Basic .NET finds a match on Number at the Case Is < 20 statement because Number is less than 20 and is not found in the first Case using the To keyword. Even though the value of Number is 1 , Visual Basic .NET never gets to the last Case when you enter 1 .

The Is keyword can be useful in a Select Case block, but it is a bit tricky to use. You need to be sure you clearly understand the ranges covered and the placement of the Case statements when you use the Is keyword in a Select Case block.

Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: