Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio .NET
Reference
Data Types
 Object Type
Visual Basic Language Concepts
Object Type

The Object data type is a 32-bit (4-byte) address that points to an object within your application or in some other application. When you declare a variable as Object, you can subsequently use it to refer to any object recognized by your application, as in the following example:

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

When you declare an object variable, try to use a specific class rather than the generic Object, for example Database instead of Object in the preceding example. You should also use the most specific class available, such as TextBox instead of Control, so that you can access its properties and methods. You can usually use the Classes list in the Object Browser to find available class names.

Variables of the Object data type can also point to data of any type. This means 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 Type class, as in the following example:

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

Programming elements declared as Object can accept values of any data type. Object is treated as a value type when holding a value type, and as a reference type when holding a reference type. In either case, an Object variable does not contain the 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.

No literal type character exists for Object.

Determining Inheritance

You can compare two objects to determine the relationship, if any, between the classes from which they are created. The IsInstanceOfType method of the Type class returns True if the current class inherits from the specified class, or if the current type is an interface supported by the specified class. The following example determines whether one object represents a class inherited from another object's class:

Public Class BaseClass
End Class
Public Class DerivedClass : Inherits BaseClass
End Class
Public Class TestTheseClasses
   Public Sub SeeIfRelated()
      Dim BaseObj As Object = New BaseClass()
      Dim DerivedObj As Object = New DerivedClass()
      Dim Related As Boolean
      Related = BaseObj.GetType().IsInstanceOfType(DerivedObj)
      MsgBox(CStr(Related))
   End Sub
End Class ' TestTheseClasses

Note the unexpected placement of the two object variables in the use of IsInstanceOfType. The supposed base type is used to generate the Type class, and the supposed derived type is passed as an argument to IsInstanceOfType.

See Also

Type Conversions | Object Variables | Type Class | Type.GetTypeCode Method | Type.IsInstanceOfType Method | Object Data Type

© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker