Flylib.com

Books Software

 
 
 

Section 5.5. Comparison Operators


5.5. Comparison Operators

There are three main comparison operators: < (less than), > (greater than), and = (equal to). They can be used individually, or any two operators can be combined with each other to form other comparison operators. The general syntax is:

result =


expression1 <operator> expression2



The result is a Boolean value of TRue or False .

The following list indicates the condition required with each VB comparison operator to return a value of TRue .



= (Equal To)

TRue if expression1 is equal to expression2



< (Less Than)

TRue if expression1 is less than (and not equal to) expression2



> (Greater Than)

true if expression1 is greater than (and not equal to) expression2



<= (Less Than or Equal To)

true if expression1 is less than or equal to expression2



>= (Greater Than or Equal To)

TRue if expression1 is greater than or equal to expression2



<> (Not Equal To)

TRue if expression1 is not equal to expression2

Comparison operators can be used with both numeric and string expressions. If one expression is numeric and the other is a string, the string is first converted to a number of type Double (nonnumeric strings throw an exception). If both expression1 and expression2 are strings, the "greatest" string is the one that appears second in sort order. The sorting is based on the current character code page in use by the application, the region-specific locale information, and the OptionCompare setting. If that setting is Binary , the comparison is case-sensitive, whereas a setting of Text results in a case-insensitive comparison.

New in 2005 . There are two "hidden" operators in Visual Basic: IsTrue( arg ) and IsFalse( arg ) . They return a Boolean value that indicates whether the supplied argument is true or False , respectively. You cannot use them directly in your code, but they do exist, beginning in the 2005 release of Visual Basic, to support operator overloading. This is covered in the "Operator Overloading" section later in this chapter.

5.5.1. The Like Operator

The Like operator is used to match a string against a pattern. It compares a string expression or literal with a string pattern expression and determines whether they match (the result is true ) or not (the result is False ). For example:

If (testString Like "[A-Z]#") Then

matches a capital letter followed by a digit.

For details on the use of this operator, including special characters used in the pattern string, see the "Like Operator" entry in Chapter 12.



5.6. Object Operators

Visual Basic includes five operators that return results based on an operand's object properties.



Is

The Is operator determines whether two object reference variables refer to the same object instance.



result


=


object1


Is


object2



If both object1 and object2 refer to the same object instance, the result is true ; otherwise , the result is False . You can also use the Is operator to determine if an object variable refers to a valid object. This is done by comparing the object variable to the Nothing keyword:

If (customerRecord Is Nothing) Then

The result is true if the object variable does not hold a reference to any object.



IsNot

New in 2005 . The IsNot operator is equivalent to the Is operator used with the Not logical operator. The statement:

If (customerRecord IsNot Nothing) Then

is the same as:

If Not (customerRecord Is Nothing) Then

There is no functional difference between the two statements. The IsNot operator was added to VB to make such statements more readable.



TypeOf

The TypeOf operator determines if an object variable is of a specific data type. It is always used with the Is operator. (It does not work with the new VB 2005 IsNot operator.) The following statement tests an object variable to see if it is an Integer .

If (TypeOf someNumber Is Integer) Then



AddressOf

The AddressOf operator returns a procedure delegate that can be used to reference a procedure through a variable. In VB6, the AddressOf operator returned a function pointer , the memory address of the function. While the .NET version of this operator serves a similar purpose, it does not return a memory address. The .NET Framework reserves the right to move objects (including procedures) to new memory locations at any time, so you cannot depend on the memory address.

For details on the AddressOf operator, including usage information, see the AddressOf Operator entry in Chapter 12.



GetType

The GetType operator returns a System.Type object that contains information about the data type of the operand. You cannot use expressions or variables as operands; you must pass a data type itself. You can use VB data types (like Integer or String ), .NET core types (like System.Int32 ), or the name of any class, structure, or similar construct. For example:

result = GetType(Integer)

returns a System.Type object that provides information about the System.Int32 data type, which is the true data type of the Visual Basic Integer data type.