MATLAB Excel Builder    

Visual Basic New Operator

This method uses the Visual Basic New operator on a variable explicitly dimensioned as the class to be created. Before using this method, you must reference the type library containing the class in the current VBA project. Do this by selecting the Tools menu from the Visual Basic editor, and then selecting References... to display the Available References list. From this list select the necessary type library.

The following example illustrates using the New operator to create a class instance. It assumes that you have selected mycomponent 1.0 Type Library from the Available References list before calling this function.

In this example, the class instance could be dimensioned as simply myclass. The full declaration in the form <component-name>.<class-name> guards against name collisions that could occur if other libraries in the current project contain types named myclass.

Both methods are equivalent in functionality. The first method does not require a reference to the type library in the VBA project, while the second results in faster code execution. The second method has the added advantage of enabling the Auto-List-Members and Auto-Quick-Info capabilities of the VBA editor to work with your classes. The default function wrappers created with each built component all use the first method for object creation.

In the previous two examples, the class instance used to make the method call was a local variable of the procedure. This creates and destroys a new class instance for each call. An alternative approach is to declare one single module-scoped class instance that is reused by all function calls, as in the initialization code of the previous example.

The following example illustrates this technique with the second method.


  Creating an Instance of a Class Calling the Methods of a Class Instance