How to: Hold Data of Unknown Type in a Variable

A variable holds pointers to data if you declare it to be of data type Object.

A variable of the Object Data Type can hold a pointer to a value of any data type. This includes both value types, such as Integer, Boolean, and structure instances, and reference types, which are instances of objects created from classes such as String, Form, and array instances.

Performance. The advantage of the Object data type is that you can use it to point to data of any data type. The disadvantage is that you incur extra operations that take more execution time and make your application perform slower. If you use an Object variable for value types, you incur boxing and unboxing. If you use it for reference types, you incur late binding.

You should use the Object data type when your variable needs to hold different data types at different times, or when you do not know at compile time what data type or types your variable might hold.

To hold data of unknown type in a variable

  1. Declare the variable with a Dim Statement (Visual Basic).

  2. Follow the variable name with an As clause.

  3. Follow the As keyword with the Object keyword.

See Also

Concepts

Data Types in Visual Basic

Type Characters

Value Types and Reference Types

Reference

Data Type Summary (Visual Basic)

Single Data Type (Visual Basic)

Object Data Type

Other Resources

Elementary Data Types