Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 5: Decision
Last change: February 13 2016 18:47:58.
<<PreviousNext>>

Nesting Select Case Blocks

You can nest Select Case blocks in the same way as the If blocks are nested. That is, within each Case of a Select Case block, you can code another Select Case block. The nesting can practically go as deep as you would like. Beware of the problem with clarity, though. As in the case of the If blocks, you should never intertwine a Select Case block with another.

An Example

As an illustration of nesting the Select Case blocks, assume that a university charges tuition based on the student’s residence status and the number of hours taken, as shown in the following table.

Hours Taken In State Out of State International
1–5 850 1,250 1,700
6–8 1,000 1,500 2,000
9–11 1,300 2,000 2,700
12–over 1,500 2,300 3,200

You are to design a project to determine the student’s tuition when the student enrolls. How should you proceed?
As usual, you will need to design the visual interface first. The student must declare a residence status among the three categories. These categories represent mutually exclusive alternatives that will most likely remain unchanged for a long while, so radio buttons will be a good choice. The number of hours taken can be entered into a text box. The visual design can appear as in Figure 5-6.

Figure 5-6
Visual Interface for Tuition Calculation

A list of the properties of the VB controls used in code is given in the following table.

Control Property Setting Remark
Radio button Text In State  
Name rbtInState  
Checked True As the default option
Radio button Text Out Of State  
Name rbtOutOfState  
Radio button Text International  
Name rbtInternational  
Text box Name txtHoursTaken To enter hours taken
Text box Name txtTuition To display tuition
ReadOnly True So that the user cannot accidentally change the computed result
Button Text Compute To initiate tuition computation
Name btnCompute  

Notice that the Checked property of the rbtInState radio button is set to True so that it can be used as the default status. When the user clicks the Compute button, your program will show the result in the txtTuition text box.
There are several ways to determine a student’s tuition. One possibility is to use the nested Select Case blocks. As you can see from the previous table, two factors jointly determine the amount of tuition: residence status and hours taken. Either of these factors can be considered the first level of branching. Suppose you use the hours taken as the first level. You can then use the following skeleton to code the problem:

Private Sub btnCompute_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim Tuition As Decimal Select Case Val(txtHoursTaken.Text) Case 1 to 5 ‘For the first bracket Case 6 To 8 ‘For the second bracket Case 9 to 11 ‘For the third bracket Case Else ’12 or more hours End Select txtTuition.Text = Format(Tuition, “Standard”) ‘Display the tuition End Sub

Within each Case, you can code the tuition based on the residence status. Should you also use the Select Case structure? You can, but it takes a bit more analysis. Instead, begin with the more straightforward If block first. Inspect the table again; under the 1-5 hours bracket, you see that a student with an in state, out of state, and international status will pay 850, 1250, 1700, respectively. The first bracket can be coded as follows:

Select Case Val(txtHoursTaken.Text) Case 1 to 5 ‘For the first bracket If rbtInState.Checked Then Tuition = 850 ElseIf rbtOutOfState.Checked Then Tuition = 1250 Else Tuition = 1700 End If Case 6 To 8 ‘For the second bracket Case 9 to 11 ‘For the third bracket Case Is >= 12  ’12 or more hours End Select

The rest of the brackets can be coded in a similar fashion and is left to you as an exercise (5-30). For simplicity, the case where the user enters a zero or nothing was also left out. The additional code to take care of this is also left to you.
Note that the If block can be conveniently used here to handle the radio buttons because the state (Checked property) of each button is a different expression. Using the Select Case structure to handle multiple expressions like this can be a challenge. By syntax, the Select Case structure is more natural to branch based on one single expression.

Identifying the Radio Button Selected

To force the use of Select Case structure to handle radio buttons, a commonly used approach is to introduce a class level variable, whose value can then be set in the Click event of each radio button. For example, when rbtInState, rbtOutOfState, or rbtInternational is clicked, the variable ResidenceStatus can be set to 0, 1, or 2. ResidenceStatus can then be used as an expression in the Select Case statement. The following code should set the value of ResidenceStatus properly.

Dim ResidenceStatus As Integer Private Sub rbtInState_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles rbtInState.Click ResidenceStatus = 0 End Sub Private Sub rbtOutOfState_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles rbtOutOfState.Click ResidenceStatus = 1 End Sub Private Sub rbtInternational_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles rbtInternational.Click ResidenceStatus = 2 End Sub

After the residence status is determined, you can add the following Select Case block to determine the tuition of the first bracket of hours taken.

Select Case ResidenceStatus Case 0 ‘In State Tuition = 850 Case 1 ‘Out of State Tuition = 1250 Case 2 ‘International Tuition = 1700 End Select

The complete code for the button Click event procedure using the nested Select Case blocks can then appear as follows:

Private Sub btnCompute_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim Tuition As Decimal Select Case Val(txtHoursTaken.Text) Case 1 To 5 ‘First bracket Select Case ResidenceStatus Case 0 ‘In State Tuition = 850 Case 1 ‘Out of State Tuition = 1250 Case 2 ‘International Tuition = 1700 End Select Case 6 To 8 ‘Second bracket Select Case ResidenceStatus Case 0 ‘In State Tuition = 1000 Case 1 ‘Out of State Tuition = 1500 Case 2 ‘International Tuition = 2000 End Select Case 9 To 11 ‘Third bracket Select Case ResidenceStatus Case 0 ‘In State Tuition = 1300 Case 1 ‘Out of State Tuition = 2000 Case 2 ‘International Tuition = 2700 End Select   Case Is >= 12 ‘12 hours or more Select Case ResidenceStatus Case 0 ‘In State Tuition = 1500 Case 1 ‘Out of State Tuition = 2300 Case 2 ‘International Tuition = 3200 End Select End Select End Select txtTuition.Text = Format(Tuition, “Standard”) ‘Display tuition End Sub

The logic in this procedure appears fairly straightforward. Identifying the radio button selected, however, takes some analysis. Incidentally, there is a trick that you can use with the Select Case structure to identify fairly easily the radio button selected, but it takes some inverted thinking. Rather than explaining its logic here, the trick will be presented in the Explore and Discover exercise (5-11) for you to examine.

More on the Syntax of Select Case

Because the tuition is jointly determined by the student’s residence status and hours taken, can you code the cases as shown here?

Select Case ResidenceStatus And HoursTaken Case 0 And 1 To 5 . . End Select  
No. Although ResidenceStatus And HoursTaken can be considered as an expression, it means something different to the computer than what a beginner may have in mind. Recall that the expression in the Select Case statement is first evaluated to arrive at a value that will be used to match against the criteria in the Cases. In an attempt to evaluate the expression, the computer will first perform a logical And operation on the variables ResidenceStatus and HoursTaken.
Suppose that ResidenceStatus has a value zero (0). No matter what value HoursTaken may be, the result of the And operation will be a zero. (Recall that zero is equal to False and the And logical operator requires that both of its operands be True for the result to be True; otherwise, the result will be False, which is a zero.) The first Case statement that has a zero will be considered the match. Assuming there are no other problems, the result will be both unexpected and unpredictable.
Now, consider this statement:

Case 0 And 1 To 5

1 To 5 is a range expression; however, an And operator is on its left, which is not expected; therefore, the statement has a syntax error.

Mixing the Select Case Structure with the If Block

As you may have already noticed in the preceding example, a Select Case structure can have If blocks within its structure. Conversely, an If block can also contain Select Case structures. There is no restriction to which structure can enclose the other as long as the nesting constitutes a relationship of the inner and outer blocks, as discussed in the preceding section.

An Alternative Design

The tuition example can be designed somewhat differently. For example, instead of using the text box to enter hours taken and radio buttons for residence status, you can use two combo boxes for both. The first combo box can be used to display the residence status; and the second one, for the brackets of hours taken. The visual interface can then appear as in Figure 5-7.

Figure 5-7
Alternative user interface design for tuition calculation

A list of the properties of the controls used in code is given in the following table.

Control Property Setting Remark
Combo box Name cboResidenceStatus  
DropDownStyle DropDownList To disallow user entry
Combo box Name cboHoursTaken  
DropDownStyle DropDownList To disallow user entry
Text box Name txtTuition To display tuition
ReadOnly True So that the user cannot accidentally change the computed result
Button Text Compute To initiate tuition computation
Name btnCompute  

In the Form Load event, the two combo boxes can be populated using the Items.Add method as follows:

Private Sub frmTuitionCalculation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load With cboResidenceStatus.Items 'Populate combo box with residence status .Add("In State") .Add("Out Of State") .Add("International") End With cboResidenceStatus.SelectedIndex = 0 'Set default status (In state) With cboHoursTaken.Items 'Populate combo box with brackets of hours taken .Add("1 to 5") .Add("6 to 8") .Add("9 to 11") .Add("12 or More") End With cboHoursTaken.SelectedIndex = 3 End Sub

Notice that you have also added statements to set the SelectedIndex properties of both combo boxes. This will make the items corresponding to the SelectedIndex values appear in the controls’ text boxes as the default selections when the program starts.
The Click event procedure for the button can then be modified with the following code skeleton:

Private Sub btnCompute_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim Tuition As Decimal Select Case cboResidenceStatus.SelectedIndex Case 0 'In state Select Case cboHoursTaken.SelectedIndex Case 0 ' 1 to 5 hours Case 1 ' 6 to 8 hours Case 2 ' 9 to 11 hours Case 3 ' 12 or more hours End Select Case 1 'Out Of state 'Similar to the Case for In state Case 2 'International 'Similar to the Case for In state End Select lblTuition.Text = Format(Tuition, "Standard") End Sub

With the comments in the structure, you should be able to complete the remaining code.
Notice that in the procedure, the residence status is used as the first-level Select Case structure. This is done to show that either of the two factors can be the first level without affecting the result. This structure should appear to you logically more natural because you typically would check the residence status before asking about the number of hours taken.
Notice also that the Select Case statements now use the combo boxes’ SelectedIndex properties as the expressions. Because the DropDownStyle properties of both controls are set to DropDownList, the user cannot enter any data into the text portion of the combo box. Instead, the user can select an item from the list. Such an action will set the controls’ SelectedIndex properties.

Which Design Do You Prefer?

The original design has the advantage of code clarity. When you review the code, you should be able to have a good feel about what the code is supposed to do; however, it is not flexible. For example, if the hours taken for each bracket change, and the number of brackets remain the same, you will need to change the program to handle the new situation. The second design has the advantage of code brevity. It is also more flexible in handling the changes in data. Although you use the Items.Add method to populate the combo boxes, in a real application, the items should be read from a file or database. A change in the hours taken for each bracket will not necessitate a revision of the program as long as the number of brackets remains the same. The code, however, is not as clear because the SelectedIndex itself says very little about the underlying data. In such a case, it is important that you include comments to provide the additional details.
There are actually other alternatives to handle this problem. Another alternative that is even more flexible is considered in an exercise in Chapter 8, “Arrays and Their Uses.” These alternatives are presented here to illustrate the use of the Select Case structure and the If block to solve a problem. Hopefully, each alternative design will inspire you to become more resourceful in identifying solutions to your next problem.

Last change: February 13 2016 18:47:58.
<<PreviousNext>>