InternetUnicodeHTMLCSSScalable Vector Graphics (SVG)Extensible Markup Language (xml) ASP.Net TOCASP.NetMiscellaneous Feature ASP.NET Scripting Visual Basic .NET TOCVB .NET Language ReferencenaVB.Net Elements OperatorArithmetic Operator Draft for Information Only
Content
VB.NET Comparison Operators
VB.NET Comparison OperatorsThe supporting VB.NET Comparison Operators are <, <=, >, >=, =, <>, Is, IsNot, Like. <, <=, >, >=, =, <>, Is, IsNot, Like OperatorsThese 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. Syntaxresult = expression1 comparisonoperator expression2 result = object1 [Is | IsNot] object2 result = string Like pattern Partsresult: 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. RemarksThe following table contains a list of the relational comparison operators and the conditions that determine whether result is True or False.
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 NumbersWhen 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 ImprecisionWhen 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 StringsWhen 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 DependenceWhen 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 OperatorsThe 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.
Numeric comparisons treat Nothing as 0. String comparisons treat Nothing as "" (an empty string). OverloadingThe 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 OperatorCompares two object reference variables. Syntaxresult = object1 Is object2 Partsresult: Required. Any Boolean value. object1: Required. Any Object name. object2: Required. Any Object name. RemarksThe 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 OperatorCompares two object reference variables. Syntaxresult = object1 IsNot object2 Partsresult: Required. A Boolean value. object1: Required. Any Object variable or expression. object2: Required. Any Object variable or expression. RemarksThe 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 OperatorCompares a string against a pattern. Important The Like operator is currently not supported in .NET Core and .NET Standard projects. Syntaxresult = string Like pattern Partsresult: 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." RemarksIf 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 MethodThe 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 OptionsBuilt-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.
Character ListsA 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 CharactersTo 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 RangesBy 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 RangesTo 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 HyphenA 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 SequenceThe 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 CharactersIn 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. OverloadingThe 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: 8/1/2020 Revision: 0 Ref: ![]() References
![]() Latest Updated Links
![]() ![]() ![]() ![]() ![]() |
![]() Home 5 Business Management HBR 3 Information Recreation Hobbies 8 Culture Chinese 1097 English 339 Travel 18 Reference 79 Computer Hardware 254 Software Application 213 Digitization 37 Latex 52 Manim 205 KB 1 Numeric 19 Programming Web 289 Unicode 504 HTML 66 CSS 65 SVG 46 ASP.NET 270 OS 431 DeskTop 7 Python 72 Knowledge Mathematics Formulas 8 Set 1 Logic 1 Algebra 84 Number Theory 206 Trigonometry 31 Geometry 34 Calculus 67 Engineering Tables 8 Mechanical Rigid Bodies Statics 92 Dynamics 37 Fluid 5 Control Acoustics 19 Natural Sciences Matter 1 Electric 27 Biology 1 |
Copyright © 2000-2025 Sideway . All rights reserved Disclaimers last modified on 06 September 2019