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.

Relational Operator

Relational operators are used to compare the operands, and decide whether the relation is true. For example, you can use the equal operator to test whether two variables, A and B, are equal by coding:

If A = B Then MsgBox(“A and B are equal”)

When A is actually equal to B, the expression is True and the message, “A and B are equal” will be displayed; otherwise, the expression will be False, and the message will not be displayed. The following table provides a list of the relational operators.

Relational Operator Example Result/Explanation
= A = B True when A is actually equal to B; otherwise, False.
<>  A <> B True when A is not equal to B; otherwise, False.
A > B True when A is greater than B; otherwise, False.
>= A >= B True when A is greater than or equal to B; otherwise, False.
A < B True when A is less than B; otherwise, False.
<= A <= B True when A is less than or equal to B; otherwise, False.

Numeric Comparisons

Typically, relational operators are used to compare numeric data. For example, the following code checks whether the hours worked in a week entered by the user is greater than or equal to 168. If so, a message box is displayed.

If Val(txtHoursWorked.Text) >= 168 Then MsgBox(“Have you ever slept?”, MsgBoxStyle.Question) txtHoursWorked.Focus End If

In the code, if the number of hours worked is found to be greater than or equal to 168, the message box displays a question mark with a message teasing the user. (Additional details concerning the use of the MsgBox statement are presented in Chapter 6, “Input, Output, and Procedures.”) The text box’s Focus method is then used to reset the focus on the text box so that the user can correct the number.

Recall that the minimum requirement for a user-friendly program is that the message should provide concise information in a friendly tone. The message displayed in the above code is not really a good example. A better message would be:
MsgBox( “The maximum number of hours allowed per week is 60.”, MsgBoxStyle.Information)
Additionally, if the upper limit is 60, the hours worked should be compared with 60, not 168.

Potential Problem with Conversion

When the operands involved are of different data types, a conversion will have to take place before the comparison is done. Occasionally, a conversion can bring about some unexpected result. Consider this example:

Const RateS As Single = 0.095 Const RateD As Double = 0.095 Private Sub btnCompare_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles btnCompare.Click Dim A As Single, D As Double A = RateS * 99895 D = RateD * 99895 If A = D Then MsgBox("A and D are equal.") Else MsgBox("A and D are not equal.") End If End Sub

If you test the above code in an event procedure, you should see the message “A and D are not equal” is displayed. Why? If you inspect the computational results, D has a value of 9490.025, but A has 9490.024. A is less accurate because its data type is Single, and is unable to represent the data with the exact precision.
Although the advice is to always use the same data type to avoid data conversion in any operations,including comparisons, you occasionally may be faced with a situation when it is necessary to perform mixed-data type comparisons. In such a case, instead of comparing for equality, you can allow a very small difference between the two values to avoid the problem. For example, you can work around the problem with the previous example by modifying the code for the comparison as follows:

If Math.Abs((A - D) / D) < 0.000001 Then

In this statement, you assume that you are willing to accept a relative difference of 10-6 as virtually equal; however, this approach will do well only if D is not close to zero. If D is anywhere near zero, you should check the absolute difference, that is, Math.Abs(A – D), instead of the relative difference.

String Comparisons

The familiar relational operators can also be used to compare string data. For example, you can compare to see whether a name entered in a text box such as txtName is the same as the variable CustName by coding the following:

If txtName.Text = CustName Then . . .

Notice that all the six relational operators can be used in string expressions. The question is how are the string data compared? For example, is A greater than a? No. The comparison is based on the keycode value of the characters. Because A has a keycode value of 65, and a has a value of 97, a is greater than A.
Note that this result is obtained based on VB’s default setting for comparison, Option Compare Binary. You can change the setting to Option Compare Text, under which the comparison is not case sensitive; that is, a and A are considered equal. Similar to other option settings, such as Option Explicit and Option Strict, you can place Option Compare Text at the beginning of a module (code window) to effect text comparison; or, you can set the option in the project Options dialog box. The steps are exactly the same as setting Option Explicit, which is described in Chapter 4, “Data, Operations, and Built-in Functions.”

Look It Up
Search the Index tab of the help file for the keyword KeyCode enumeration member. It shows the predefined keycode names of most keys.

Comparing Strings with Unequal Lengths

What happens when two strings are not of the same length? Basically, the comparison is carried out from left to right in pairs, one character after another until an inequality is found or one of the strings runs out of characters; therefore, the string “b” is greater than “abcd” because the first character in the first string, “b” is greater than the first character in the second string. Also, the string “ab” is greater than “a”. The latter string runs out of characters before an inequality can be determined.

When you set Option Compare Text, all string comparisons will no longer be case sensitive. In some cases, you may want to preserve the case sensitive comparison (that is, set Option Compare Binary), but occasionally need to perform character comparison that is not case sensitive. In such cases, you can use the UCase or LCase functions before the comparison is performed. For example, the code:
If UCase(txtName.Text) = UCase(CustName)  Then …
will result in True for a name McCord and MCCORD, or any variations of upper- or lowercase spellings on either side of the equality comparison.
Last change: February 13 2016 18:48:00.