变体类型的 VBA 数组作为类属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5342666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Array of variant type as class property
提问by RomnieEE2
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:
具体来说,这种类型的 double array-property 工作正常,可以一次接收或返回一个数组:
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:
但是当我对类型变体的数组尝试相同的模式时,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
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?
但是,在属性中,适用于 Dim D() As Double 的模式不适用于 Dim V() As Variant,这是众所周知的标准吗?
采纳答案by Tomalak
Public Property Get VariantArray() As Variant()
VariantArray = p_vaVariantArray()
End Property
Note the missing parentheses.
请注意缺少的括号。