Sideway
output.to from Sideway
Draft for Information Only

Content

VB.NET Comparison Operators
  <, <=, >, >=, =, <>, Is, IsNot, Like Operators
  Syntax
  Parts
  Remarks
  Comparing Numbers
  Floating-point Imprecision
  Comparing Strings
  Locale Dependence
  Typeless Programming with Relational Comparison Operators
  Overloading
  See also
  Is Operator
  Syntax
  Parts
  Remarks
  See also
  IsNot Operator
  Syntax
  Parts
  Remarks
  See also
  Like Operator
  Syntax
  Parts
  Remarks
  Comparison Method
  Pattern Options
  Character Lists
  Special Characters
  Character Ranges
  Multiple Character Ranges
  Usage of the Hyphen
  Collating Sequence
  Digraph Characters
  Overloading
 See also
  Source/Reference

VB.NET Comparison Operators

The supporting VB.NET Comparison Operators are <, <=, >, >=, =, <>, Is, IsNot, Like.

<, <=, >, >=, =, <>, Is, IsNot, Like Operators

These operators compare two expressions to determine whether or not they are equal, and if not, how they differ. Is, IsNot, and Like are discussed in detail on separate Help pages. The relational comparison operators are discussed in detail on this page.

Syntax

      result = expression1 comparisonoperator expression2  
result = object1 [Is | IsNot] object2  
result = string Like pattern  

Parts

result: Required. A Boolean value representing the result of the comparison.

expression" Required. Any expression.

comparisonoperator: Required. Any relational comparison operator.

object1, object2: Required. Any reference object names.

string: Required. Any String expression.

pattern: Required. Any String expression or range of characters.

Remarks

The following table contains a list of the relational 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 = Operator is also used as an assignment operator.

The Is operator, the IsNot operator, and the Like operator have specific comparison functionalities that differ from the operators in the preceding table.

Comparing Numbers

When you compare an expression of type Single 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 you compare an expression of type Decimal to an expression of type Single or Double, the Decimal expression is converted to Single or Double. For 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.

Floating-point Imprecision

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.

Comparing Strings

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)

Locale Dependence

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 expression1 or 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
Both String Sort comparison based on string sorting characteristics.
Both numeric Objects converted to Double, numeric comparison.
One numeric 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 is thrown.
Either or both are reference types other than String An InvalidCastException is thrown.

Numeric comparisons treat Nothing as 0. String comparisons treat Nothing as "" (an empty string).

Overloading

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.

See also

Is Operator

Compares two object reference variables.

Syntax

result = object1 Is object2  

Parts

result: Required. Any Boolean value.

object1: Required. Any Object name.

object2: Required. Any Object name.

Remarks

The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons. If object1 and object2 both refer to the exact same object instance, result is True; if they do not, result is False.

Is can also be used with the TypeOf keyword to make a TypeOf...Is expression, which tests whether an object variable is compatible with a data type.

Note

The Is keyword is also used in the Select...Case Statement.

See also

IsNot Operator

Compares two object reference variables.

Syntax

result = object1 IsNot object2  

Parts

result: Required. A Boolean value.

object1: Required. Any Object variable or expression.

object2: Required. Any Object variable or expression.

Remarks

The IsNot operator determines if two object references refer to different objects. However, it does not perform value comparisons. If object1 and object2 both refer to the exact same object instance, result is False; if they do not, result is True.

IsNot is the opposite of the Is operator. The advantage of IsNot is that you can avoid awkward syntax with Not and Is, which can be difficult to read.

You can use the Is and IsNot operators to test both early-bound and late-bound objects.

Note

The IsNot operator cannot be used to compare expressions returned from the TypeOf operator. Instead, you must use the Not and Is operators.

See also

Like Operator

Compares a string against a pattern.

Important

The Like operator is currently not supported in .NET Core and .NET Standard projects.

Syntax

result = string Like pattern  

Parts

result: Required. Any Boolean variable. The result is a Boolean value indicating whether or not the string satisfies the pattern.

string: Required. Any String expression.

pattern: Required. Any String expression conforming to the pattern-matching conventions described in "Remarks."

Remarks

If the value in string satisfies the pattern contained in pattern, result is True. If the string does not satisfy the pattern, result is False. If both string and pattern are empty strings, the result is True.

Comparison Method

The behavior of the Like operator depends on the Option Compare Statement. The default string comparison method for each source file is Option Compare Binary.

Pattern Options

Built-in pattern matching provides a versatile tool for string comparisons. The pattern-matching features allow you to match each character in string against a specific character, a wildcard character, a character list, or a character range. The following table shows the characters allowed in pattern and what they match.

Characters in pattern Matches in string
? Any single character
* Zero or more characters
# Any single digit (0–9)
[charlist] Any single character in charlist
[!charlist] Any single character not in charlist

Character Lists

A group of one or more characters (charlist) enclosed in brackets ([ ]) can be used to match any single character in string and can include almost any character code, including digits.

An exclamation point (!) at the beginning of charlist means that a match is made if any character except the characters in charlist is found in string. When used outside brackets, the exclamation point matches itself.

Special Characters

To match the special characters left bracket ([), question mark (?), number sign (#), and asterisk (*), enclose them in brackets. The right bracket (]) cannot be used within a group to match itself, but it can be used outside a group as an individual character.

The character sequence [] is considered a zero-length string (""). However, it cannot be part of a character list enclosed in brackets. If you want to check whether a position in string contains one of a group of characters or no character at all, you can use Like twice. For an example, see How to: Match a String against a Pattern.

Character Ranges

By using a hyphen (–) to separate the lower and upper bounds of the range, charlist can specify a range of characters. For example, [A–Z] results in a match if the corresponding character position in string contains any character within the range A–Z, and [!H–L] results in a match if the corresponding character position contains any character outside the range H–L.

When you specify a range of characters, they must appear in ascending sort order, that is, from lowest to highest. Thus, [A–Z] is a valid pattern, but [Z–A] is not.

Multiple Character Ranges

To specify multiple ranges for the same character position, put them within the same brackets without delimiters. For example, [A–CX–Z] results in a match if the corresponding character position in string contains any character within either the range A–C or the range X–Z.

Usage of the Hyphen

A hyphen (–) can appear either at the beginning (after an exclamation point, if any) or at the end of charlist to match itself. In any other location, the hyphen identifies a range of characters delimited by the characters on either side of the hyphen.

Collating Sequence

The meaning of a specified range depends on the character ordering at run time, as determined by Option Compare and the locale setting of the system the code is running on. With Option Compare Binary, the range [A–E] matches A, B, C, D, and E. With Option Compare Text, [A–E] matches A, a, À, à, B, b, C, c, D, d, E, and e. The range does not match Ê or ê because accented characters collate after unaccented characters in the sort order.

Digraph Characters

In some languages, there are alphabetic characters that represent two separate characters. For example, several languages use the character æ to represent the characters a and e when they appear together. The Like operator recognizes that the single digraph character and the two individual characters are equivalent.

When a language that uses a digraph character is specified in the system locale settings, an occurrence of the single digraph character in either pattern or string matches the equivalent two-character sequence in the other string. Similarly, a digraph character in pattern enclosed in brackets (by itself, in a list, or in a range) matches the equivalent two-character sequence in string.

Overloading

The Like operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures.

See also

 

Source/Reference


©sideway

ID: 200800001 Last Updated: 1/8/2020 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 149

Software

Application 198

Digitization 25

Latex 7

Manim 115

Numeric 19

Programming

Web 283

Unicode 494

HTML 65

CSS 58

ASP.NET 149

OS 389

DeskTop 7

Python 19

Knowledge

Mathematics

Formulas 8

Algebra 25

Number Theory 206

Trigonometry 18

Geometry 21

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Physics

Electric 27

Biology 1

Geography 1


Copyright © 2000-2020 Sideway . All rights reserved Disclaimers last modified on 06 September 2019