作为类属性的变体类型的 VBA 数组
我有一个类,它处理多个数值数组(双精度类型),并且还需要处理描述符数组,其中包括字符串和整数的混合,需要相应地用作字符串和数字。所以我决定创建一个变体类型的数组属性(不是包含数组的变体)。但这似乎不起作用,而双精度数组却可以。
具体来说,这种类型的双数组属性工作得很好,可以一次接收或返回一个数组:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意缺少的括号。
Note the missing parentheses.