Object Variable Assignment (Visual Basic)
You use a normal assignment statement to assign an object to an object variable. You can assign an object expression or the Nothing keyword, as the following example illustrates.
means there is no object currently assigned to the variable.
When your code begins running, your object variables are initialized to . Those whose declarations include initialization are reinitialized to the values you specify when the declaration statements are executed.
You can include initialization in your declaration by using the New keyword. The following declaration statements declare object variables and and assign specific objects to them. Each uses one of the overloaded constructors of the appropriate class to initialize the object.
Setting an object variable to discontinues the association of the variable with any specific object. This prevents you from accidentally changing the object by changing the variable. It also allows you to test whether the object variable points to a valid object, as the following example shows.
If the object your variable refers to is in another application, this test cannot determine whether that application has terminated or just invalidated the object.
An object variable with a value of is also called a null reference.
The current instance of an object is the one in which the code is currently executing. Since all code executes inside a procedure, the current instance is the one in which the procedure was invoked.
The keyword acts as an object variable referring to the current instance. If a procedure is not Shared, it can use the keyword to obtain a pointer to the current instance. Shared procedures cannot be associated with a specific instance of a class.
Using is particularly useful for passing the current instance to a procedure in another module. For example, suppose you have a number of XML documents and wish to add some standard text to all of them. The following example defines a procedure to do this.
Every XML document object could then call the procedure and pass its current instance as an argument. The following example demonstrates this.
Object Variable Declaration
Object Variable Values
How to: Declare an Object Variable and Assign an Object to It in Visual Basic
How to: Make an Object Variable Not Refer to Any Instance
Me, My, MyBase, and MyClass
Object Data Type
Holds addresses that refer to objects. You can assign any reference type (string, array, class, or interface) to an variable. An variable can also refer to data of any value type (numeric, , , , structure, or enumeration).
The data type can point to data of any data type, including any object instance your application recognizes. Use when you do not know at compile time what data type the variable might point to.
The default value of is (a null reference).
You can assign a variable, constant, or expression of any data type to an variable. To determine the data type an variable currently refers to, you can use the GetTypeCode method of the System.Type class. The following example illustrates this.
The data type is a reference type. However, Visual Basic treats an variable as a value type when it refers to data of a value type.
Whatever data type it refers to, an variable does not contain the data 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, variables holding value types are slightly slower to access than explicitly typed variables.
Interop Considerations. If you are interfacing with components not written for the .NET Framework, for example Automation or COM objects, keep in mind that pointer types in other environments are not compatible with the Visual Basic type.
Performance. A variable you declare with the type is flexible enough to contain a reference to any object. However, when you invoke a method or property on such a variable, you always incur late binding (at run time). To force early binding (at compile time) and better performance, declare the variable with a specific class name, or cast it to the specific data type.
When you declare an object variable, try to use a specific class type, for example OperatingSystem, instead of the generalized type. 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.
Widening. All data types and all reference types widen to the data type. This means you can convert any type to without encountering a System.OverflowException error.
However, if you convert between value types and , Visual Basic performs operations called boxing and unboxing, which make execution slower.
Type Characters. has no literal type character or identifier type character.
Framework Type. The corresponding type in the .NET Framework is the System.Object class.
The following example illustrates an variable pointing to an object instance.
Type Conversion Functions
Efficient Use of Data Types
How to: Determine Whether Two Objects Are Related
How to: Determine Whether Two Objects Are Identical