Visual Basic (.NET) Is and IsNot

This article covers the Is and IsNot operators in Visual Basic.NET.

Is Operator

It compares two object reference variables and checks to see if both the object references refer to the same object without performing value comparisons. If they refer to the same object instance, the Boolean value True is returned, other wise False.

IsNot Operator

It compares two object reference variables and checks to see if both the object references refer to different objects without performing value comparisons. If they refer to the different object instances, the Boolean value True is returned, other wise False.


Examples

Remember, we need to compare object references, not the actual object itself. In the example below, we use the Object data type to declare two objects, object1 and object2. We then proceed to create three references to these two objects called Objectref1, Objectref2 and Objectref3.

Dim Object1 As New Object
Dim Object2 As New Object

Dim Objectref1, Objectref2, Objectref3 as New Object
Dim result As Boolean

Objectref1 = Object1
Objectref2 = Object1
Objectref3 = Object2

result = Objectref1 Is Objectref2
Console.writeline(result)
result = Objectref1 Is Objectref3
Console.writeline(result)

result = Objectref1 IsNot Objectref2
Console.writeline(result)
result = Objectref1 IsNot Objectref3
Console.writeline(result)

As Objectref1 and Objectref2 refer to the same object, when the Is Operator was applied, the result was True. Likewise, applying IsNot returns False. Comparing Objectref1 and Objectref3 returns False with Is and True with IsNot.

True
False
False
True

This marks the end of the Visual Basic Is and IsNot article. Suggestions or contributions for CodersLegacy are more than welcome. Any questions you may have can be asked in the comments section below.

Leave a Comment