You use a normal declaration statement to declare an object variable, but for the data type, you specify either Object or the object class. The object class is the specific class from which the object is to be instantiated. When you declare a variable with a specific object class, it can access all the methods and properties exposed by that class. If you declare the variable with Object, it can use only the members of the Object class, unless you turn off the type-checking switch (Option Strict Statement).
Use the following syntax to declare an object variable:
Dim variablename As [New] {objectclass | Object} You can also use Protected, Friend, Protected Friend, Private, Shared, or Static for the declaration. The following declarations are valid:
Private ObjA As Object ' Declare ObjA as generic Object data type.
Static ObjB As Label ' Declare ObjB as Label class type.
Dim ObjC As System.Buffer ' Declare ObjC as Buffer class type.
Sometimes the object class is not known until the procedure runs. In this case, you must declare the object variable with the Object data type. This creates a generic reference to any type of object. It also limits your code to the methods and properties of System.Object when Option Strict is turned on.
When you know the specific object class, you should declare the object variable to be of that class. In the preceding example declarations, if the application uses objects of class Label, you should specify As Label in the declarations for both ObjA and ObjB.
Declaring an object variable as a specific object class gives you several advantages:
- Automatic type checking
- Microsoft IntelliSense support in the Code editor
- Improved readability
- Fewer errors in your code
- Faster code execution
Access to Object Variable Members
When Option Strict is turned on, an object variable can access only the methods and properties of the class with which you declared it. The following example illustrates this:
' Option statements must precede all other source file lines.
Option Strict On
' Imports statement must precede all declarations in the source file.
Imports System.Windows.Forms
Dim P As Object
Dim Q As Label
P = New Label ' Assign Label class to Object declaration.
Q = New Label ' Assign Label class to Label declaration.
' ...
Dim J, K As Integer
J = P.Left ' Generates a compiler error.
K = Q.Left ' Returns left edge of label in pixels.
In this example, P can use only the members of the Object class itself, which do not include the Left property. On the other hand, Q was declared to be of type Label, so it can use all the methods and properties of the Label class in the System.Windows.Forms namespace.
Flexibility of Object Variables
When working with objects in an inheritance hierarchy, you have a choice of which class to use for declaring your object variables. In making this choice, you must balance flexibility of object assignment against access to a class's members. For example, consider the inheritance hierarchy that leads to the Form class:
Object Class
MarshalByRefObject
MarshalByRefComponent
Control Class
RichControl
ScrollableControl
ContainerControl
Form Class
Suppose your application defines a form called Form2, which inherits from class Form. You can declare an object variable that refers specifically to Form2, as follows:
Dim MyForm As New Form2 ' Can assign only Form2 objects to MyForm.
This limits the variable MyForm to objects of class Form2, but it also makes all the methods and properties of Form2 available to MyForm, as well as all the members of all the classes from which Form2 inherits.
You can make an object variable more general by declaring it to be of type Form, as follows:
Dim AnyForm As Form ' Can assign any form, but no other control.
This allows you to assign any form in your application to AnyForm. However, although AnyForm can access all the members of class Form, it cannot use any of the additional methods or properties defined for specific forms such as Form2, unless you turn off Option Strict.
See Also
Object Variables | Object Variable Assignment | Object Variable Values | Typeless Programming | System Namespace | System.Windows.Forms Namespace | Object Class | Control Class | Form Class | Label Class | New