Object Data Type - Visual Basic (original) (raw)

Holds addresses that refer to objects. You can assign any reference type (string, array, class, or interface) to an Object variable. An Object variable can also refer to data of any value type (numeric, Boolean, Char, Date, structure, or enumeration).

The Object data type can point to data of any data type, including any object instance your application recognizes. Use Object when you do not know at compile time what data type the variable might point to.

The default value of Object is Nothing (a null reference).

Data Types

You can assign a variable, constant, or expression of any data type to an Object variable. To determine the data type an Object variable currently refers to, you can use the GetTypeCode method of the System.Type class. The following example illustrates this.

Dim myObject As Object
' Suppose myObject has now had something assigned to it.
Dim datTyp As Integer
datTyp = Type.GetTypeCode(myObject.GetType())

The Object data type is a reference type. However, Visual Basic treats an Object variable as a value type when it refers to data of a value type.

Storage

Whatever data type it refers to, an Object variable does not contain the data value itself, but rather a pointer to the value. It always uses four bytes in computer memory, but this does not include the storage for the data representing the value of the variable. Because of the code that uses the pointer to locate the data, Object variables holding value types are slightly slower to access than explicitly typed variables.

Programming Tips

Example

The following example illustrates an Object variable pointing to an object instance.

Dim objDb As Object
Dim myCollection As New Collection()
' Suppose myCollection has now been populated.
objDb = myCollection.Item(1)

See also