Specifying a Range of Values in a Case Clause

In the preceding example you saw a simple Select Case statement that uses one expression in each Case clause. Many times, however, you may want to specify a range of values in a Case clause. Do this by using the To keyword between the values of expressions, as in the following example:

Select Case unitsSold Case 1 to 100

Discount = 0.1 Case 501 to 1000

Discount = 0.15 Case Is >1000

Discount = 0.2

End Select

Let's analyze the above Select Case block with the assumption that the variable unitsSold currently holds the value 99. Visual Basic compares the value of the variable unitsSold with the conditional expression in the Case clauses. The first and third Case clauses illustrate how to use a range of values in a conditional expression by using the To keyword. Because unitsSold = 99, the condition in the first Case clause is true; thus, Visual Basic assigns the value 0.05 to the variable Discount.

How about the second Case clause, which is also true? Although it's obvious that 99 is less than or equal to 500, Visual Basic does not execute the associated statement Discount = 0.1. The reason for this is that once Visual Basic locates a Case clause with a true condition, it doesn't bother to look at the remaining Case clauses. It jumps over them and continues to execute the procedure with the instructions that may be following the End Select statement.

To get more practice with the Select Case statement, let's use it for a change in a function procedure. As you recall from Chapter 4, function procedures allow you to return a result to a subroutine. Suppose a subroutine has to display a discount based on the number of units sold.

You can get the number of units from the user and then run a function to figure out which discount applies.

1. Enter the following subroutine in the SelectCase module:

Sub DisplayDiscount()

Dim unitsSold As Integer Dim myDiscount As Single unitsSold = InputBox("Enter the number of sold units:") myDiscount = GetDiscount(unitsSold) MsgBox myDiscount End Sub

2. Enter the following function procedure: Function GetDiscount(unitsSold As Integer)

Select Case unitsSold

Case 1 To 200

GetDiscount =

0.

05

Case Is <=500

GetDiscount =

0.

1

Case 501 To 1000

GetDiscount =

0.

15

Case Is >1000

GetDiscount =

0.

2

End Select End Function

End Select End Function

3. Place the cursor anywhere within the code of the DisplayDiscount procedure and press F5 to run it.

The DisplayDiscount procedure passes the value stored in the variable unitsSold to the GetDiscount function. When Visual Basic encounters the Select Case statement, it checks whether the value of the first Case clause expression matches the value stored in unitsSold. If there is a match, Visual Basic assigns a 5 percent discount (0.05) to the function name, and then jumps to the End Select keywords. Because there are no more statements to execute inside the function procedure, Visual Basic returns to the calling procedure—DisplayDiscount. Here it assigns the function's result to the variable myDiscount. The last statement displays the value of the retrieved discount in a message box.

0 0

Post a comment