VBA Array of variant type as class property

不想你离开。 提交于 2020-01-04 01:25:09

问题


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.



来源:https://stackoverflow.com/questions/5342666/vba-array-of-variant-type-as-class-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!