IsReference Function

   
IsReference Function

Class

Microsoft.VisualBasic.Information

Syntax

 IsReference(   expression   ) 
expression (required; any)

Return Value

Boolean

Description

Returns True if expression contains reference type data, as opposed to value type data

Rules at a Glance

  • IsReference returns False if expression is one of the value data types (Byte, Short, Integer, Long, Single, Double, Boolean, Date, or Char).

  • IsReference returns True if expression is a reference data type (String or Object), including an object of a specific type, such as a Collection object.

  • IsReference returns True if expression is an array, since an array is a reference type.

  • IsReference returns False if expression is a structure, since a structure is a value type.

Example

 Private Class CEmployee ... End Class ' The following message will display Dim obj As Object If IsReference(obj) Then     MsgBox("obj is reference type, but is Nothing") End If ' The following message will display ' (CEmployee is a class module) Dim c As New CEmployee(  ) If IsReference(c) Then     MsgBox("c is reference type") End If ' The following message does NOT display Dim i As Integer = 4 If IsReference(i) Then     MsgBox("Integer is reference type") End If 

Programming Tips and Gotchas

Just because a variable has been declared to be of type Object does not mean that the IsReference function will return True when that variable is passed to it as an argument. Consider the following code:

 Dim oObj As Object Console.WriteLine(IsReference(oObj))       'Returns True oObj = New CEmployee Console.WriteLine(IsReference(oObj))       'Returns True oObj = 3 Console.WriteLine(IsReference(oObj))       'Returns False oObj = "This is a string" Console.WriteLine(IsReference(oObj))       'Returns True 

In other words, the IsReference function returns True only if a variable of type Object is Nothing or if its data subtype is one of the reference types (that is, an instance of a class or a string). If its data subtype is a value type, the function returns False .

VB.NET/VB 6 Differences

The IsReference function is new to VB.NET.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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