Udostępnij za pośrednictwem


Object as the Universal Data Type

The Object Data Type is the root type in the .NET Framework and in Visual Basic. This means that all other data types and object types are derived from it, either directly or ultimately. It also means that any other data type, whether elementary or composite, can be converted to Object.

Loose Typing

You can use Object as the universal data type. This is called loose typing. The following example illustrates this flexibility.

Dim v As Object
v = "17"
' v contains the 2-character String value "17".
v = v - 15
' v now contains the Integer value 2.
v = "H" & v
' v now contains the 2-character String value "H2".

While you can perform such operations on Object variables without much concern for the kind of data they contain, you must avoid the following traps:

  • If you perform arithmetic operations or functions on an Object, it must hold numerical data or a run-time error occurs.

  • If you are concatenating strings, use the & operator instead of the + operator. There are complex rules governing when the + operator adds its operands and when it concatenates them. The + operator also must perform type checking and conversion in some cases, which reduces performance.

Changing an Object Variable's Data Type

Although an Object variable can accept data of any type, you cannot change the variable itself to a different data type. However, you can redimension an Object variable to an array of Object variables. The following example illustrates both valid and invalid uses of the ReDim Statement (Visual Basic).

' The following statement declares a single Object.
Dim someObj As Object
' The following reallocation is valid only for Object.
ReDim someObj(8)
' The following statement attempts an INVALID change of data type.
ReDim someObj(8) As Double

The first ReDim statement changes someObj to an array of type Object. This is valid only with the Object data type. The second ReDim statement is invalid because it involves a different data type. You can achieve the desired effect by using a separate array. The following example illustrates this.

' First allocate a separate array.
Dim someArray(8) As Double
' Then assign the new array to the Object variable.
someObj = someArray

Object as an Alternative to a Structure

Because the Object data type can store many different types of data, you can use an Object array in many situations where you might expect to use a structure. An Object array is somewhat more flexible than a structure, because you can change the type of data you store in each element at any time, and you can make the array dynamic so you can change its size as necessary. However, an Object array uses more memory than an equivalent structure, and its performance is slower.

See Also

Concepts

Typeless Programming in Visual Basic

Implicit and Explicit Declaration

Type Checking in Visual Basic

Efficient Use of Data Types

Object Variable Declaration

Object Variable Assignment

Object Variable Values

Reference

Data Type Summary (Visual Basic)

Type Conversion Functions

Other Resources

Type Conversions in Visual Basic