User`s guide

Creating an Instance of a Class
Function foo(x1 As Variant, x2 As Variant) As Variant
Dim aClass As mycomponent.myclass
On Error Goto Handle_Error
Set aClass = New mycomponent.myclass
' (call some methods on aClass)
Exit Function
Handle_Error:
foo = Err.Description
End Function
In this example, the class instance could be dimensioned as simply myclass.
Thefulldeclarationintheform
<component-name>.<class-name> guards
against name collisions that could occur if other libraries in the current
project contain types named
myclass.
Both methods a re 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 e xecution. 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 v ariable of the procedure. This creates and destroys a new
class instance for each call. An alternative approach is to declar e 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:
Dim aClass As mycomponent.myclass
Function foo(x1 As Variant, x2 As Variant) As Variant
On Error Goto Handle_Error
If aClass Is Nothing Then
Set aClass = New mycomponent.myclass
End If
' (call some methods on aClass)
Exit Function
2-7