Forum Discussion

yingkai1208's avatar
yingkai1208
Contributor
11 years ago
Solved

How to use a class in another script

In a script A, I declare a Class

Class animal

    Dim name

    Dim category

End Class

 

I want to use this class in another script B

 

‘USEUNIT A

Dim chick

Set chick = New animal

 

Some error occurs, could anyone have a good solution for this issue

 

Thanks in advance

                                                                       

3 Replies

  • For other novices who have the same question, the following code is the solution of Create an instance in other unit:

    [UnitA]

    Class MyClass

      ...

    End Class

     

    ' This routine creates and returns a new class instance

    Function GetClassInstance

      Set GetClassInstance = New MyClass

    End Function

     

     

     

     [UnitB]

    'USEUNIT UnitA

    Sub AnotherRoutine

      ...

      ' Calling a routine defined in UnitA.

      ' Do not forget to include this unit with the USEUNIT statement

      Set cls = GetClassInstance ' OK !

      ...

    End Sub