我尝试过解决这个问题,但找不到任何解决方案。我在普通模块中定义了一个 UDT,并希望将其用作类模块中的 Public Sub
中的参数。然后我得到一个编译错误:
Only public user defined types defined in public object modules can be used as parameters or return type for public procedures of class modules or as fields of public user defined types
然后,我尝试在类中移动我的 UDT,声明为 Private
。我收到此编译错误:
Private Enum and user defined types cannot be used as parameters or return types for public procedures, public data members, or fields of public user defined types.
我最终尝试在类中将其声明为 Public
,并收到此编译错误:
Cannot define a Public user-defined type within a private object module.
那么有没有办法让公共(public) UDT 用作类中公共(public)子中的参数?
最佳答案
只需将子定义为 Friend
范围即可。这对我来说在 VB6 类中编译得很好。
Private Type testtype
x As String
End Type
Friend Sub testmethod(y As testtype)
End Sub
从您的错误消息看来,您的类(class)是私有(private)的。如果您确实希望您的类是公共(public)的 - 即您正在创建 ActiveX exe 或 DLL 并且您希望客户端能够访问子类 - 那么只需将类型和子类都设为公共(public)即可。
关于vba - 用户定义类型 (UDT) 作为类模块 (VB6) 中公共(public) Sub 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/975982/