COMPARISON OPERATORS

Comparison operators compare one value to another and return a Boolean value (True or False), depending on the result. The following table lists the comparison operators provided by Visual Basic. The first six (=, <>, <, <=, >, and >=) are relatively straightforward. Note that the Not operator is not a comparison operator, so it is not listed here. It is described in the next section, “Logical Operators.”

image

image

The Is operator returns True if its two operands refer to the same object. For example, if you create an Order object and make two different variables, A and B, point to it, the expression A Is B is True. Note that Is returns False if the two operands point to different Order objects that happen to have the same property values.

The IsNot operator is simply shorthand for a more awkward Not . . . Is construction. For example, the statement A IsNot Nothing is equivalent to Not (A Is Nothing).

The value Nothing is a special value that means not an object. If you have an object variable, you can use the Is or IsNot operator to compare it to Nothing to see if it represents anything. Note that you cannot use Is or IsNot to compare an object variable to 0 or some other numeric value. Is and IsNot work only with objects such as those stored in variables and the special value Nothing.

The TypeOf operator returns True if its operand is of a certain type or inherits from that type. This operator is particularly useful when a subroutine takes a parameter that could be of more than one object type. It can use TypeOf to see which type of object it has.

The Like operator returns True if its first operand matches a pattern specified by its second operand. Where the pattern includes normal characters, the string must match those characters exactly. The pattern can also include several special character sequences summarized in the following table.

CHARACTER(S) MEANING
? Matches any single character
* Matches any zero or more characters
# Matches any single digit
[characters] Matches any of the characters between the brackets
[!characters] Matches any character not between the brackets
A-Z When inside brackets, matches any character in the range A to Z

You can combine ranges of characters and individual characters inside brackets. For example, the pattern [a-zA-Z] matches any letter between a and z or between A and Z. The following table lists some useful patterns for use with the Like operator.

PATTERN MEANING
[2-9]##-#### Seven-digit U.S. phone number
[2-9]##-[2-9]##-#### Ten-digit phone number, including area code
1-[2-9]##-[2-9]##-#### Eleven-digit phone number, beginning with 1 and area code
##### Five-digit ZIP code
#####-#### Nine-digit ZIP + 4 code
?*@?*.?* E-mail address

For example, the following code checks whether the text box txtPhone contains something that looks like a 10-digit phone number:

If Not (txtPhone.Text Like "[2-9]##-[2-9]##-####") Then
    MessageBox.Show("Please enter a valid phone number")
End If

These patterns are not completely foolproof. For example, the e-mail address pattern verifies that the string contains at least one character, an @ character, at least one other character, a dot, and at least one more character. For example, it allows [email protected]. However, it does not verify that the extension makes sense, so it also allows [email protected], and it allows more than one @ character, as in [email protected]@bad_value.

Regular expressions provide much more powerful pattern-matching capabilities. For an introduction to regular expressions, see http://www.codeproject.com/Articles/939/An-Introduction-to-Regular-Expressions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181