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
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.

The If…Then…ElseIf…Then…Else…End If Block

Many times, a decision can be much more complicated than that in the previous example; that is, several conditions, rather than just the either or situation, will determine the conclusion. For example, a teacher may assign an A to any score greater than or equal to 90, a B if the score is greater than or equal to 80, and a C to all other scores. Is there an If block that can handle this situation conveniently? Yes! The syntax appears as follows:

If Condition1 Then Statements to be executed when condition1 is True ElseIf Condntion2 Then Statements to be executed when condition2 is True ElseIf Condition3 Then Statements to be executed when condition3 is True Else Statements to be executed when none of the above conditions are True End If

In this structure, you can have as many ElseIf statements as desired following the If statement. You can then code a catch all Else statement after all of the ElseIf statements. It is an error to place an ElseIf after an Else statement. Again, the entire block is closed with an End If statement.
When the block is executed, the conditions in the If block are tested one after another top down (condition1, condition2, and so on) until one condition is true. At that point, those statements under that condition are executed, and the remainder of the If block is skipped. If none of the conditions are true, the statements in the Else portion (if any) are executed.
Using this structure, the teacher’s grade assignment problem can be coded as follows:

If Score >= 90 Then Grade = “A” ElseIf Score >= 80 Then Grade = “B” Else Grade = “C” End If

The code will first check whether Score is greater than or equal to 90. If so, A is assigned to Grade and the remainder of the If block is ignored. If the first condition is not true, the ElseIf statement checks to see whether Score is greater than or equal to 80. If so, B is assigned to Grade and the remaining part of the If block is ignored. On the other hand, if the condition in the ElseIf statement is not true, the statement in the Else block will be executed; that is, C is assigned to Grade.

Beware! The following code will not obtain the desired results:
If Score < 80 Then Grade = “C” ElseIf Score >= 80 Then Grade = “B” ElseIf Score >= 90 Then Grade = “A” End If
Why? If the score is less than 80, a C is assigned; otherwise, the ElseIf Score >= 80 will be always true, resulting in a B being assigned. There will be no A even if the score is 100. When you are coding this If structure, always test the most restrictive condition first.

Varying the Criteria

Of course, this structure can be used to handle more complex situations. Consider this investment decision example. An independent oil exploration company will invest in an oil exploration project if a pretest indicates that the probability of finding oil is at least 50%; otherwise, the net present value of the project will have to be greater than $1 million. If not, the company will invest only if the project will cost less than $100,000. The decision rule can be depicted as in Figure 5-1.

Figure 5-1
Decision rules for the investment decision

Note that unlike the previous example, at each stage of the decision, a different factor is involved in this problem. This, however, poses no problem to this If structure. Assume Invest is a Boolean variable indicating whether to invest. Also, the variables, Probability, NPV, and Cost, have been assigned proper values. One way to solve the problem is to code the following:

If Probability >= .5 Then Invest = True ‘Invest when the probability of success is high ElseIf NPV >= 1000000 Then Invest = True ‘Invest when the expected return is high ElseIf Cost <= 100000 Then Invest = True ‘Invest when the cost is low Else Invest = False ‘ don’t invest if none of the above is true End If

A Key Validation Procedure: Another Example

Similar to the previous “If Then Else” block, any part of the “If Then ElseIf Then” structure may leave out statements without causing a syntax problem. To illustrate, consider a data-entry key validation routine. You would like to be sure that keys entered into a text box named txtNumber are numbers. If not, you want to display an error message and suppress the key. How do you code this?
As you may recall, when the user presses a key, the KeyPress event is triggered and the KeyChar of the key is also passed as one of the event arguments to this event procedure. In coding this event, you should inspect the key to see if it is numeric.

Be aware that not all the keys that are nonnumeric are bad keys for this data validation purpose. The control keys, such as the Enter key and the Backspace key, are also captured in the KeyPress event. All these control keys have a keycode value lower than 32 (the keycode value of the spacebar, a constant named Keys.Space). You should also display an error message, and suppress the key if it does not belong to either of the previous two groups of keys.
How do you check for a numeric key? The Keys enumeration provides constant names for all keys. The keycode values of the keys 0 and 9 are Keys.D0 (48) and Keys.D9 (57), respectively. You can compare the keycode value of the key pressed to see if it is in this range. One way to implement the key validation procedure is as follows:

Private Sub txtNumber_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber.KeyPress Dim KeyAscii As Integer KeyAscii = AscW(e.KeyChar) ‘Convert the character to a numeric keycode If KeyAscii < Keys.Space Then ' These are control keys, ignore. ElseIf KeyAscii >= Keys.D0 AndAlso KeyAscii <= Keys.D9 Then ' These are numeric keys, ignore. Else ' These are neither control keys nor numeric keys. ' Display an error message MsgBox("Numeric keys only, please!") e.Handled = True ' Suppress the key End If End Sub

In this code example, no statement appears in either the If or the ElseIf portion of the If block; however, the code works exactly the way you want it to. The key pressed by the user (KeyChar) is first converted to an integer variable (KeyAscii) using the AscW function, which gives the numeric value of a key in unicode. The KeyAscii value is first compared with Keys.Space (32). If the value is less than 32, the key is a control key; otherwise (the ElseIf block), the value is checked to see whether it is within the range of numeric keys. If not (the Else block), an error message is displayed. In addition, the event’s Handled argument is assigned a value of True. Assigning True to this argument tells the system that the key has been handled properly and not to process it further. This, in effect, suppresses the key, and no key will appear in the text box.

Do you want to know the keycode values of the numeric keys? Try the following code in the form click event:
Dim I As Integer For I = Asc(“0”) To Asc(“9”) MsgBox(Chr(I) & “ “ & I) Next I

You can also find the keycode value of a key name in your code by resting the mouse pointer on the key name. For example, if you have Keys.D0 in your code, just rest the mouse pointer on D0. You should see the IntelliSense shows a declared constant value of 48.

Last change: February 13 2016 18:47:59.