Comparison Operators (Visual Basic) (2024)

  • Article

Comparison operators compare two expressions and return a Boolean value that represents the relationship of their values. There are operators for comparing numeric values, operators for comparing strings, and operators for comparing objects. All three types of operators are discussed herein.

Comparing Numeric Values

Visual Basic compares numeric values using six numeric comparison operators. Each operator takes as operands two expressions that evaluate to numeric values. The following table lists the operators and shows examples of each.

OperatorCondition testedExamples
= (Equality)Is the value of the first expression equal to the value of the second?23 = 33 ' False

23 = 23 ' True

23 = 12 ' False

<> (Inequality)Is the value of the first expression unequal to the value of the second?23 <> 33 ' True

23 <> 23 ' False

23 <> 12 ' True

< (Less than)Is the value of the first expression less than the value of the second?23 < 33 ' True

23 < 23 ' False

23 < 12 ' False

> (Greater than)Is the value of the first expression greater than the value of the second?23 > 33 ' False

23 > 23 ' False

23 > 12 ' True

<= (Less than or equal to)Is the value of the first expression less than or equal to the value of the second?23 <= 33 ' True

23 <= 23 ' True

23 <= 12 ' False

>= (Greater than or equal to)Is the value of the first expression greater than or equal to the value of the second?23 >= 33 ' False

23 >= 23 ' True

23 >= 12 ' True

Comparing Strings

Visual Basic compares strings using the Like Operator as well as the numeric comparison operators. The Like operator allows you to specify a pattern. The string is then compared against the pattern, and if it matches, the result is True. Otherwise, the result is False. The numeric operators allow you to compare String values based on their sort order, as the following example shows.

"73" < "9"

' The result of the preceding comparison is True.

The result in the preceding example is True because the first character in the first string sorts before the first character in the second string. If the first characters were equal, the comparison would continue to the next character in both strings, and so on. You can also test equality of strings using the equality operator, as the following example shows.

"734" = "734"

' The result of the preceding comparison is True.

If one string is a prefix of another, such as "aa" and "aaa", the longer string is considered to be greater than the shorter string. The following example illustrates this.

"aaa" > "aa"

' The result of the preceding comparison is True.

The sort order is based on either a binary comparison or a textual comparison depending on the setting of Option Compare. For more information see Option Compare Statement.

Comparing Objects

Visual Basic compares two object reference variables with the Is Operator and the IsNot Operator. You can use either of these operators to determine if two reference variables refer to the same object instance. The following example illustrates this.

Dim x As testClassDim y As New testClass()x = yIf x Is y Then ' Insert code to run if x and y point to the same instance.End If

In the preceding example, x Is y evaluates to True, because both variables refer to the same instance. Contrast this result with the following example.

Dim x As New customer()Dim y As New customer()If x Is y Then ' Insert code to run if x and y point to the same instance.End If

In the preceding example, x Is y evaluates to False, because although the variables refer to objects of the same type, they refer to different instances of that type.

When you want to test for two objects not pointing to the same instance, the IsNot operator lets you avoid a grammatically clumsy combination of Not and Is. The following example illustrates this.

Dim a As New classA()Dim b As New classB()If a IsNot b Then ' Insert code to run if a and b point to different instances.End If

In the preceding example, If a IsNot b is equivalent to If Not a Is b.

Comparing Object Type

You can test whether an object is of a particular type with the TypeOf...Is expression. The syntax is as follows:

TypeOf <objectexpression> Is <typename>

When typename specifies an interface type, then the TypeOf...Is expression returns True if the object implements the interface type. When typename is a class type, then the expression returns True if the object is an instance of the specified class or of a class that derives from the specified class. The following example illustrates this.

Dim x As System.Windows.Forms.Buttonx = New System.Windows.Forms.Button()If TypeOf x Is System.Windows.Forms.Control Then ' Insert code to run if x is of type System.Windows.Forms.Control.End If

In the preceding example, the TypeOf x Is Control expression evaluates to True because the type of x is Button, which inherits from Control.

For more information, see TypeOf Operator.

See also

  • Value Comparisons
  • Comparison Operators
  • Operators
  • Arithmetic Operators in Visual Basic
  • Concatenation Operators in Visual Basic
  • Logical and Bitwise Operators in Visual Basic
Comparison Operators (Visual Basic) (2024)
Top Articles
Free 2D Free Floor Plan Maker | Wondershare EdrawMax
Why Your LinkedIn Posts Get Low Engagement!
My E Chart Elliot
Odawa Hypixel
The Pope's Exorcist Showtimes Near Cinemark Hollywood Movies 20
123 Movies Black Adam
House Share: What we learned living with strangers
3472542504
Inevitable Claymore Wow
Connect U Of M Dearborn
iZurvive DayZ & ARMA Map
Scotchlas Funeral Home Obituaries
Costco Great Oaks Gas Price
Uconn Health Outlook
Outlet For The Thames Crossword
CVS Near Me | Columbus, NE
Cbssports Rankings
Culver's Flavor Of The Day Taylor Dr
Poe Str Stacking
Menus - Sea Level Oyster Bar - NBPT
yuba-sutter apartments / housing for rent - craigslist
Happy Homebodies Breakup
Accuweather Minneapolis Radar
Horn Rank
Parkeren Emmen | Reserveren vanaf €9,25 per dag | Q-Park
Ou Football Brainiacs
Jailfunds Send Message
What we lost when Craigslist shut down its personals section
Dell 22 FHD-Computermonitor – E2222H | Dell Deutschland
Miles City Montana Craigslist
1964 Impala For Sale Craigslist
Free Tiktok Likes Compara Smm
Wcostream Attack On Titan
What Happened To Father Anthony Mary Ewtn
Cruise Ships Archives
Jefferson Parish Dump Wall Blvd
Ishow Speed Dick Leak
KM to M (Kilometer to Meter) Converter, 1 km is 1000 m
Planet Fitness Lebanon Nh
8 Ball Pool Unblocked Cool Math Games
5A Division 1 Playoff Bracket
Craigslist Malone New York
Differential Diagnosis
QVC hosts Carolyn Gracie, Dan Hughes among 400 laid off by network's parent company
Arginina - co to jest, właściwości, zastosowanie oraz przeciwwskazania
60 Days From August 16
Mlb Hitting Streak Record Holder Crossword Clue
Convert Celsius to Kelvin
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5995

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.