Is Operator


Is Operator

Syntax

     Dim result As Boolean = object1 Is { object2 | Nothing } 


object1 (required; Object or any reference type)

An object or instance for comparison


object2 (required; Object or any reference type)

An object or instance for comparison

Description

The Is operator compares two object references and indicates whether they refer to the same underlying instance (true) or not (False). It is most often used with If...Then...Else statements, as in:

     If (someVariable Is someOtherVariable) Then        ' ----- Equivalent-specific code here.     End If 

Comparing a variable with Nothing tests whether an instance has not yet been assigned to that variable.

     If (someVariable Is Nothing) Then        ' ----- The variable is undefined.     End If 

Usage at a Glance

  • Both object1 and object2 must be reference-type variables. This includes string variables, object variables, and array variables. You can call the IsReference function to ensure that both object1 and object2 are reference types.

  • The Is operator can be used with the TypeOf operator to determine if an object reference is of a specific type.

  • The Is operator reports that uninitialized reference types are equal. For example, the following comparison equates to true:

         Dim emptyForm As Windows.Forms.Form     Dim ordinaryString As String     If (emptyForm Is ordinaryString) Then MsgBox("Same")        ' ----- The MsgBox will indeed appear. 

Version Differences

In .NET, strings and arrays are reference types. In VB 6, strings and arrays are not reference types and, therefore, cannot be used with the Is operator.

See Also

IsNot Operator, TypeOf Operator




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net