重载属性

发布于 2024-10-21 13:15:06 字数 468 浏览 7 评论 0原文

我尝试在类模块内的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小兔几 2024-10-28 13:15:06

您可以使用变体或对象类型并手动处理类型检查吗?

可选参数的混合也是可能的,但它并不完全等同于重载。

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.

梦晓ヶ微光ヅ倾城 2024-10-28 13:15:06

不可以。

解决方法是使用变体,它允许客户端传递任何内容。但是您必须编写代码来在运行时进行类型检查:

Public Property Let SomeNumber(newNumber as Variant)
   Select Case VarType(newNumber) 
      Case vbInteger         '' Caller passed an integer
        a = newNumber
      Case vbString          '' Caller passed a string
        a = Val(newNumber)
      Case Else
        Err.Raise vbObjectError+513, , "Invalid type passed"
      End Select
End Property
Public Property Get SomeNumber() As Variant
   SomeNumber = a
End Property

有关更多详细信息,请参阅 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:

Public Property Let SomeNumber(newNumber as Variant)
   Select Case VarType(newNumber) 
      Case vbInteger         '' Caller passed an integer
        a = newNumber
      Case vbString          '' Caller passed a string
        a = Val(newNumber)
      Case Else
        Err.Raise vbObjectError+513, , "Invalid type passed"
      End Select
End Property
Public Property Get SomeNumber() As Variant
   SomeNumber = a
End Property

For more details see Dan Appleman's excellent book Developing COM/ActiveX Components In Visual Basic 6.

怎樣才叫好 2024-10-28 13:15:06

从我在这里可以看到:

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.

德意的啸 2024-10-28 13:15:06

有一个关于此主题的主题:

Excel VBA 中的函数重载和 UDF

There was a thread on this topic:

Function Overloading and UDF in Excel VBA

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文