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
Does Living On A Budget Mean You're Broke?
Wealthy Affiliate University - Why Should You Join? Find Out Here.
Devotion Showtimes Near Xscape Theatres Blankenbaker 16
Ups Customer Center Locations
Bucks County Job Requisitions
Gameplay Clarkston
Chase Claypool Pfr
Violent Night Showtimes Near Amc Fashion Valley 18
True Statement About A Crown Dependency Crossword
Ucf Event Calendar
Shooting Games Multiplayer Unblocked
8 Ways to Make a Friend Feel Special on Valentine's Day
Illinois Gun Shows 2022
St Maries Idaho Craigslist
Army Oubs
97226 Zip Code
Clare Briggs Guzman
Seeking Arrangements Boston
Cardaras Funeral Homes
Blackboard Login Pjc
Pensacola Tattoo Studio 2 Reviews
Pixel Combat Unblocked
27 Fantastic Things to do in Lynchburg, Virginia - Happy To Be Virginia
Lcsc Skyward
CohhCarnage - Twitch Streamer Profile & Bio - TopTwitchStreamers
Downloahub
Helpers Needed At Once Bug Fables
Craigslist Free Stuff San Gabriel Valley
Workboy Kennel
Roch Hodech Nissan 2023
Jennifer Reimold Ex Husband Scott Porter
Quake Awakening Fragments
Planet Fitness Lebanon Nh
Felix Mallard Lpsg
Electronic Music Duo Daft Punk Announces Split After Nearly 3 Decades
Metro Pcs Forest City Iowa
Bob And Jeff's Monticello Fl
All-New Webkinz FAQ | WKN: Webkinz Newz
ESA Science & Technology - The remarkable Red Rectangle: A stairway to heaven? [heic0408]
Rocky Bfb Asset
Pain Out Maxx Kratom
Trending mods at Kenshi Nexus
Sapphire Pine Grove
Lebron James Name Soundalikes
Oefenpakket & Hoorcolleges Diagnostiek | WorldSupporter
F9 2385
Wvu Workday
How to Get a Check Stub From Money Network
Naughty Natt Farting
Ranking 134 college football teams after Week 1, from Georgia to Temple
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.