Share via


How to: Create an Object 

An object is an instance of a class. To make use of the class's members you must first create an object from that class.

To create an object from a class

  1. Determine from which class you want to create an object.

  2. Write a Dim Statement (Visual Basic) to create a variable to which you can assign a class instance. The variable should be of the type of the desired class.

    Dim nextCustomer As customer 
    
  3. Add the New (Visual Basic) keyword to initialize the variable to a new instance of the class.

    Dim nextCustomer As New customer
    
  4. You can now access the members of the class through the object variable.

    nextCustomer.accountNumber = lastAccountNumber + 1
    

Robust Programming

Whenever possible, you should declare the variable to be of the class type you intend to assign to it. This is called early binding. If you do not know the class type at compile time, you can invoke late binding by declaring the variable to be of the Object Data Type. However, late binding can make performance slower and limit access to the run-time object's members. For more information, see Object Variable Declaration.

See Also

Tasks

How to: Reuse a Working Component
How to: Define a Class That Uses Members of an Existing Class
How to: Access Shared and Nonshared Members of an Object

Concepts

Objects and Classes