VBA中自定义类实现含自定义字段接口报错问题咨询
VBA接口使用自定义对象类型报错的解决方法
你的问题出在VBA接口的成员声明和实现类的属性过程不匹配上:
接口声明错误:
VBA中接口类不能直接用Public User As User这种字段式声明定义对象类型成员,必须显式声明Property Get和Property Set方法——因为对象类型的属性需要通过Set关键字完成赋值。修正后的Rate接口代码:
Public Property Get User() As User End Property Public Property Set User(ByVal value As User) End Property实现类属性过程错误:
你在实现类里用了Property Let,但Let仅适用于String、Long这类值类型,对象类型的属性赋值必须用Property Set,这就是调试报错的核心原因。修正后的RateStudent类代码:
Implements Rate Private m_User As User Public Property Set Rate_User(ByVal value As User) Set m_User = value End Property Public Property Get Rate_User() As User Set Rate_User = m_User End Property
User类代码保持不变即可:
Public id As Long Public name As String
这样修改后,接口和实现类的签名就能匹配,自定义User类型的属性可以正常工作。
内容的提问来源于stack exchange,提问作者Light
相关产品推荐
相关产品推荐

