重载属性
我尝试在类模块内的 VBA 中重载:
Dim a As Integer
Public Property Let SomeNumber(newNumber as Integer)
a = newNumber
End Property
Public Property Let SomeNumber(newNumber as String)
a = Val(newNumber)
End Property
Public Property Get SomeNumber() As Integer
SomeNumber = a
End Property
编译器抱怨“检测到不明确的名称”,其中显然存在不同的签名。是否可以重载 VBA 或 VB6 类中定义的属性?如果是这样,语法是什么?
此外,如果不可能进行属性重载,那么除了以更无缝的方式访问实例化对象的字段之外,属性相对于公共函数定义的 get/set 方法还有什么好处呢?
I tried to overload in VBA within a class module:
Dim a As Integer
Public Property Let SomeNumber(newNumber as Integer)
a = newNumber
End Property
Public Property Let SomeNumber(newNumber as String)
a = Val(newNumber)
End Property
Public Property Get SomeNumber() As Integer
SomeNumber = a
End Property
The compiler complains that an "ambiguous name was detected" where clearly there is a different signature. Is it possible to overload a property defined within a class in VBA or VB6? If so, what would be the syntax?
Moreover, if property overloading is not possible, what benefits do properties offer over get/set methods defined by public functions other than a more seamless way to access the fields of an instantiated object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用变体或对象类型并手动处理类型检查吗?
可选参数的混合也是可能的,但它并不完全等同于重载。
Can you use a variant or object type and manually handle the type checking?
A mixture of Optional parameters may also be possible, but it's not exactly equivalent to overloading either.
不可以。
解决方法是使用变体,它允许客户端传递任何内容。但是您必须编写代码来在运行时进行类型检查:
有关更多详细信息,请参阅 Dan Appleman 的优秀书籍 在 Visual Basic 6 中开发 COM/ActiveX 组件。
No.
The workaround is to use Variants, which allows the client to pass anything. But you have to write code to do the type-checking at runtime:
For more details see Dan Appleman's excellent book Developing COM/ActiveX Components In Visual Basic 6.
从我在这里可以看到:
http://vbcity.com/forums/t/76453.aspx< /a>
看来你不走运,直到你转换到 VB.NET。
From what I can see here:
http://vbcity.com/forums/t/76453.aspx
It seems you are out of luck until you convert to VB.NET.
有一个关于此主题的主题:
Excel VBA 中的函数重载和 UDF
There was a thread on this topic:
Function Overloading and UDF in Excel VBA