Name
Is Operator
Syntax
object1
Isobject2
-
object1
Use: Required
Data Type: Object or any reference type
-
object2
Use: Required
Data Type: Object or any reference type
Return Value
Boolean
Description
Compares two object variables or reference variables to determine whether they reference the same object
Rules at a Glance
Both
object1
andobject2
must be reference-type variables. This includes string variables, object variables, and array variables, for instance.The operation returns a result of
True
if the references are identical andFalse
if they are not.It is also possible to determine whether an object contains a valid reference by replacing
object2
with the specialNothing
keyword. For example:If
oDrive
Is Nothing Then
returns
True
ifoDrive
does not refer to an object andFalse
if it does. This is the only method that should be used to test for an uninitialized object reference.
Programming Tips and Gotchas
You can call the
IsReference
function to ensure that bothobject1
andobject2
are reference types.You may wonder why there is a special
Is
operator for reference types. When you perform a comparison of scalar variables, you want to know if their values are the same. But in the case of objects, you want to know if two references point to a single object. (Since many objects have identical property values, a test for equal values is meaningless.) Hence, theIs
operator is used for this purpose.Typically, the
Is
operator is used in anIf...Then...Else
construct to take some action ...
Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.