Object Variable Values (Visual Basic)

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

A variable of the Object Data Type can refer to data of any type. The value you store in an Object variable is kept elsewhere in memory, while the variable itself holds a pointer to the data.

Visual Basic supplies functions that return information about what an Object variable refers to, as shown in the following table.

FunctionReturns True if the Object variable refers to
IsArrayAn array of values, rather than a single value
IsDateA Date Data Type value, or a string that can be interpreted as a date and time value
IsDBNullAn object of type DBNull, which represents missing or nonexistent data
IsErrorAn exception object, which derives from Exception
IsNothingNothing, that is, no object is currently assigned to the variable
IsNumericA number, or a string that can be interpreted as a number
IsReferenceA reference type (such as a string, array, delegate, or class type)

You can use these functions to avoid submitting an invalid value to an operation or a procedure.

You can also use the TypeOf Operator to determine whether an object variable currently refers to a specific data type. The TypeOf...Is expression evaluates to True if the run-time type of the operand is derived from or implements the specified type.

The following example uses TypeOf on object variables referring to value and reference types.

' The following statement puts a value type (Integer) in an Object variable.  
Dim num As Object = 10  
' The following statement puts a reference type (Form) in an Object variable.  
Dim frm As Object = New Form()  
If TypeOf num Is Long Then Debug.WriteLine("num is Long")  
If TypeOf num Is Integer Then Debug.WriteLine("num is Integer")  
If TypeOf num Is Short Then Debug.WriteLine("num is Short")  
If TypeOf num Is Object Then Debug.WriteLine("num is Object")  
If TypeOf frm Is Form Then Debug.WriteLine("frm is Form")  
If TypeOf frm Is Label Then Debug.WriteLine("frm is Label")  
If TypeOf frm Is Object Then Debug.WriteLine("frm is Object")  

The preceding example writes the following lines to the Debug window:

num is Integer

num is Object

frm is Form

frm is Object

The object variable num refers to data of type Integer, and frm refers to an object of class Form.

You can declare and use an array of Object variables. This is useful when you need to handle a variety of data types and object classes. All the elements in an array must have the same declared data type. Declaring this data type as Object allows you to store objects and class instances alongside other data types in the array.

Object Variables
Object Variable Declaration
Object Variable Assignment
How to: Refer to the Current Instance of an Object
How to: Determine What Type an Object Variable Refers To
How to: Determine Whether Two Objects Are Related
How to: Determine Whether Two Objects Are Identical
Data Types

Show: