Visual Basic Language Reference  

Comparison Operators

Compares expressions.

result = expression1 comparisonoperator expression2
result = object1 Is object2
result = string Like pattern

Parts

result
Required. Any numeric variable. The result is a Boolean value representing the result of the comparison.
expression
Required. Any expression.
comparisonoperator
Required. Any comparison operator.
object
Required. Any reference object name.
string
Required. Any String expression.
pattern
Required. Any String expression or range of characters.

Remarks

The following table contains a list of the comparison operators and the conditions that determine whether result is True or False.

Operator True if False if
< (Less than) expression1 < expression2 expression1 >= expression2
<= (Less than or equal to) expression1 <= expression2 expression1 > expression2
> (Greater than) expression1 > expression2 expression1 <= expression2
>= (Greater than or equal to) expression1 >= expression2 expression1 < expression2
= (Equal to) expression1 = expression2 expression1 <> expression2
<> (Not equal to) expression1 <> expression2 expression1 = expression2
Note   The Is and Like operators have specific comparison functionality that differs from the operators in the table.

When an expression of type Single is compared to one of type Double, the Single expression is converted to Double. This behavior is opposite to the behavior found in Visual Basic 6.

Similarly, when an expression of type Decimal is compared with an expression of type Single or Double, the Decimal expression will be converted to Single or Double. For Decimal expressions, any fractional value less than 1E-28 may be lost. Such fractional value loss may cause two values to compare as equal when they are not. For this reason, care should be taken when using equality (=) to compare two floating point variables. It is better to test that the absolute value of the difference between the two numbers is less than a small acceptable tolerance.

Comparing Strings

When Strings are compared, the string expressions are evaluated based on their alphabetical sort order. The sort order for strings is evaluated based upon the Option Compare setting.

Option Compare Binary results in string comparisons based on a sort order derived from the internal binary representations of the characters. Sort order is determined by the code page. In the following example, a typical binary sort order is shown:

A < B < E < Z < a < b < e < z < ? < ? < ? < ? < ? < ?

Option Compare Text results in string comparisons based on a case-insensitive, textual sort order determined by your system's locale. When you sort the same characters using Option Compare Text, the following text sort order is produced:

(A=a) < (?=?) < (B=b) < (E=e) < (?=?) < (?=?) < (Z=z)

Typeless programming with comparison operators

The use of comparison operators with Object expressions is not allowed under Option Strict On. When Option Strict is Off, and expression1 or expression2 are Object expressions, their runtime type determines how they are compared. The following table shows how the expressions are compared and the result from the comparison, depending on the runtime type of the operands:

If operands are: Comparison will be:
Both strings Sort comparison based on string sorting characteristics.
Both numeric values Objects will be converted to Double, and numeric comparison performed.
One numeric value, and one String The String is converted to a Double and numeric comparison is performed. If the String cannot be converted to Double, an InvalidCastException will be thrown.
Either or both are reference types other than String An InvalidCastException will be thrown.

For numeric comparisons, Nothing is treated as 0. For string comparisons, Nothing is treated as "".

Example

This example shows various uses of comparison operators, which you use to compare expressions. Comparison operators return a Boolean result that represents whether or not the stated expression is true. When the > and < operators are applied to strings, the comparison is made between the normal alphabetical sorting order of the strings. This may be dependent on locale. Whether the sort is case-sensitive or not depends on the Option Compare setting.

Dim myResult As Boolean
myResult = 45 < 35   ' Returns False.
myResult = 45 = 45   ' Returns True.
myResult = 4 <> 3   ' Returns True.
myResult = "5" > "4444"   ' Returns True.

See Also

= Operator | Is Operator | Like Operator | Option Compare Statement | Operator Precedence in Visual Basic | Operators Listed by Functionality | Option Compare Statement | Comparison Operators