VBA Array of variant type as class property

2019-07-06 22:28发布

问题:

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?

回答1:

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

Note the missing parentheses.