How to: Declare an Object Variable and Assign an Object to It in Visual Basic

You declare a variable of the Object Data Type by specifying As Object in a Dim Statement. You assign an object to such a variable by placing the object after the equal sign (=) in an assignment statement or initialization clause.

Example

The following example declares an Object variable and assigns the current instance to it.

Dim thisObject As Object
thisObject = "This is an Object"

You can combine the declaration and assignment by initializing the variable as part of its declaration. The following example is equivalent to the preceding example.

Dim thisObject As Object= "This is an Object"

Compile the code

This example requires:

  • A reference to the System namespace.

  • A class, structure, or module in which to put the Dim statement.

  • A procedure in which to put the assignment statement.

See also