C.4 Comparison OperatorsThere 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. Their general syntax is: result = expression1 <operator> expression2 The resulting expression is True (-1), False (0), or Null . A Null results if and only if either expression1 or expression2 itself is Null . What follows is a list of all the comparison operators supported by VB.NET, as well as an explanation of the condition required for the comparison to result in True :
Comparison operators can be used with both numeric and string variables . However, if one expression is numeric and the other is a string, the numeric expression will always be "less than" the string expression. If both expression1 and expression2 are strings, the "greatest" string is the one that is the longest. If the strings are of equal length, the comparison is based on the value of the Option Compare setting. If its value is Binary , the comparison is case sensitive. (Lowercase letters are "greater" than their uppercase counterparts.) If its value is Text , the comparison is not case sensitive. C.4.1 The Is OperatorWhile not strictly a comparison operator, the Is operator determines whether two object reference variables refer to the same object. Thus, in some sense, it tests for the "equality" of two object references. Its syntax is: result = object1 Is object2 If both object1 and object2 refer to the same object, 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 special Nothing data type: If oVar Is Nothing Then The result is True if the object variable does not hold a reference to an object. C.4.2 The Like OperatorThe Like operator is used to match strings. It compares a string variable or string literal with a pattern expression and determines whether they match (the result is True ) or not (the result is False ). For more on this operator, see Like Operator in Chapter 10. |