Comparison Operators (Visual Basic)
The following are the comparison operators defined in Visual Basic.
These operators compare two expressions to determine whether or not they are equal, and if not, how they differ.
Like are discussed in detail on separate Help pages. The relational comparison operators are discussed in detail on this page.
result = expression1 comparisonoperator expression2 result = object1 [Is | IsNot] object2 result = string Like pattern
Boolean value representing the result of the comparison.
Required. Any expression.
Required. Any relational comparison operator.
Required. Any reference object names.
String expression or range of characters.
The following table contains a list of the relational comparison operators and the conditions that determine whether
The = Operator is also used as an assignment operator.
Is operator, the
IsNot operator, and the
Like operator have specific comparison functionalities that differ from the operators in the preceding table.
When you compare an expression of type
Single to one of type
Single expression is converted to
Double. This behavior is opposite to the behavior found in Visual Basic 6.
Similarly, when you compare an expression of type
Decimal to an expression of type
Decimal expression is converted to
Decimal expressions, any fractional value less than 1E-28 might be lost. Such fractional value loss may cause two values to compare as equal when they are not. For this reason, you should take care when using equality (
=) to compare two floating-point variables. It is safer to test whether the absolute value of the difference between the two numbers is less than a small acceptable tolerance.
When you work with floating-point numbers, keep in mind that they do not always have a precise representation in memory. This could lead to unexpected results from certain operations, such as value comparison and the Mod Operator. For more information, see Troubleshooting Data Types.
When you compare strings, the string expressions are evaluated based on their alphabetical sort order, which depends on the
Option Compare setting.
Option Compare Binary bases string comparisons on a sort order derived from the internal binary representations of the characters. The sort order is determined by the code page. The following example shows a typical binary sort order.
A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø
Option Compare Text bases string comparisons on a case-insensitive, textual sort order determined by your application's locale. When you set
Option Compare Text and sort the characters in the preceding example, the following text sort order applies:
(A=a) < (À= à) < (B=b) < (E=e) < (Ê= ê) < (Ø = ø) < (Z=z)
When you set
Option Compare Text, the result of a string comparison can depend on the locale in which the application is running. Two characters might compare as equal in one locale but not in another. If you are using a string comparison to make important decisions, such as whether to accept an attempt to log on, you should be alert to locale sensitivity. Consider either setting
Option Compare Binary or calling the StrComp, which takes the locale into account.
Typeless Programming with Relational Comparison Operators
The use of relational comparison operators with
Object expressions is not allowed under
Option Strict On. When
Option Strict is
Off, and either
expression2 is an
Object expression, the run-time types determine 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 is|
||Sort comparison based on string sorting characteristics.|
|Both numeric||Objects converted to
|One numeric and one
|Either or both are reference types other than
||An InvalidCastException is thrown.|
Numeric comparisons treat
Nothing as 0. String comparisons treat
"" (an empty string).
The relational comparison operators (
<>) can be overloaded, which means that a class or structure can redefine their behavior when an operand has the type of that class or structure. If your code uses any of these operators on such a class or structure, be sure you understand the redefined behavior. For more information, see Operator Procedures.
Notice that the = Operator can be overloaded only as a relational comparison operator, not as an assignment operator.
The following example shows various uses of relational comparison operators, which you use to compare expressions. Relational comparison operators return a
Boolean result that represents whether or not the stated expression evaluates to
True. When you apply the
< operators to strings, the comparison is made using the normal alphabetical sorting order of the strings. This order can be dependent on your locale setting. Whether the sort is case-sensitive or not depends on the Option Compare setting.
Dim x As testClass Dim y As New testClass() x = y If x Is y Then ' Insert code to run if x and y point to the same instance. End If
In the preceding example, the first comparison returns
False and the remaining comparisons return
Send feedback about: