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

Open table as spreadsheet

Operator

Purpose

Example

Result

=

Equals

A = B

True if A equals B

<>

Not equals

A <> B

True if A does not equal B

<

Less than

A < B

True if A is less than B

<=

Less than or equal to

A <= B

True if A is less than or equal to B

>

Greater than

A > B

True if A is greater than B

>=

Greater than or equal to

A >= B

True if A is greater than or equal to B

Is

Equality of two objects

emp Is mgr

True if emp and mgr refer to the same object

IsNot

Inequality of two objects

emp IsNot mgr

True if emp and mgr refer to different objects

TypeOf...Is

Object is of a certain type

TypeOf(obj) Is Manager

True if obj points to a Manager object

Like

Matches a text pattern

A Like “-#“

True if A contains three digits, a dash, and four digits

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 AIsBis 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 only work 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. 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.

Open table as spreadsheet

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.

Open table as spreadsheet

Pattern

Meaning

[2-9]##-####

Seven-digit 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

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 RodStephens@vb-helper.com. However, it does not verify that the extension makes sense, so it allows RodStephens@vb-helper.commercial, and it allows more than one @ character, as in RodStephens@vb-helper.com@bad_value.

Regular expressions provide much more powerful pattern-matching capabilities. For more information, look up “regular expressions” in the online help.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net