作为类属性的变体类型的 VBA 数组

发布于 2024-10-23 16:47:31 字数 1111 浏览 6 评论 0原文

我有一个类,它处理多个数值数组(双精度类型),并且还需要处理描述符数组,其中包括字符串和整数的混合,需要相应地用作字符串和数字。所以我决定创建一个变体类型的数组属性(不是包含数组的变体)。但这似乎不起作用,而双精度数组却可以。

具体来说,这种类型的双数组属性工作得很好,可以一次接收或返回一个数组:

Private p_dbNumericArray() As Double

Public Property Let NumericArray(Value() As Double)
    p_dbNumericArray() = Value()
End Property
Public Property Get NumericArray() As Double()
    NumericArray() = p_dbNumericArray()
End Property

但是当我尝试使用类型变体的数组相同的模式时,Get 属性返回一个空/未分配的变体数组:

Private p_vaVariantArray() As Variant

Public Property Let VariantArray(Value() As Variant)
    p_vaVariantArray() = Value()
End Property
Public Property Get VariantArray() As Variant()
    VariantArray() = p_vaVariantArray()
End Property

将数组包装在变体(而不是具有类型变体的数组)当然可以正常工作:

Private p_vaVariantArray As Variant

Public Property Let VariantArray(Value As Variant)
    p_vaVariantArray = Value
End Property
Public Property Get VariantArray() As Variant
    VariantArray = p_vaVariantArray
End Property

但是,在属性中,适用于 Dim D() As Double 的模式不适用于 Dim V() As Variant 的模式是否已知且标准?

I have a class that handles several numeric arrays (type double) and also needs to handle an array of descriptors, which will include a mix of strings and integers, which need to be utilized as strings and numbers accordingly. So I decide to make an array property of type variant (not a variant containing an array). But this one does not seem to work, while the type double arrays do.

Specifically, this type double array-property works fine, to receive or return an array all at once:

Private p_dbNumericArray() As Double

Public Property Let NumericArray(Value() As Double)
    p_dbNumericArray() = Value()
End Property
Public Property Get NumericArray() As Double()
    NumericArray() = p_dbNumericArray()
End Property

But when I try the same pattern with an array of type variant, the Get property returns an empty/unallocated variant array:

Private p_vaVariantArray() As Variant

Public Property Let VariantArray(Value() As Variant)
    p_vaVariantArray() = Value()
End Property
Public Property Get VariantArray() As Variant()
    VariantArray() = p_vaVariantArray()
End Property

Wrapping an array in a variant (instead of having an array of type variant), of course works fine:

Private p_vaVariantArray As Variant

Public Property Let VariantArray(Value As Variant)
    p_vaVariantArray = Value
End Property
Public Property Get VariantArray() As Variant
    VariantArray = p_vaVariantArray
End Property

But is it known and standard that the pattern that works for Dim D() As Double does not work for Dim V() As Variant, in properties?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦晓ヶ微光ヅ倾城 2024-10-30 16:47:31
Public Property Get VariantArray() As Variant()
    VariantArray = p_vaVariantArray()
End Property

请注意缺少的括号。

Public Property Get VariantArray() As Variant()
    VariantArray = p_vaVariantArray()
End Property

Note the missing parentheses.

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