Creating Object Variables
You can treat anexactly the same as the to which it refers. You can set or return the of the object or use any of its .
To create an object variable:
- Declare the object variable.
- Assign the object variable to an object.
Declaring an Object Variable
Use the Dim statement or one of the other declaration statements (Public, Private, or Static) to declare an object variable. Athat refers to an object must be a Variant, an Object, or a specific type of object. For example, the following declarations are valid:
|If you use an object variable without declaring it first, theof the object variable is Variant by default.|
You can declare an object variable with the Object data type when the specificis not known until the procedure runs. Use the Object data type to create a generic reference to any object.
If you know the specific object type, you should declare the object variable as that object type. For example, if the application contains a Sample object type, you can declare an object variable for that object using either of these statements:
Declaring specific object types provides automatic type checking, faster code, and improved readability.
Assigning an Object Variable to an Object
Use the Set statement to assign an object to an object variable. You can assign anor Nothing. For example, the following object variable assignments are valid:
You can combine declaring an object variable with assigning an object to it by using the Newwith the Set statement. For example:
Setting an object variable equal to Nothing discontinues the association of the object variable with any specific object. This prevents you from accidentally changing the object by changing the variable. An object variable is always set to Nothing after closing the associated object so you can test whether or not the object variable points to a valid object. For example:
Of course, this test can never determine with absolute certainty whether or not a user has closed the application containing the object to which the object variable refers.
Referring to the Current Instance of an Object
Use the Me keyword to refer to the current instance of the object where the code is running. All procedures associated with the current object have access to the object referred to as Me. Using Me is particularly useful for passing information about the current instance of an object to a procedure in another module. For example, suppose you have the following procedure in a module:
You can call the procedure and pass the current instance of the object as an argument using the following statement: