2012-03-13 65 views
2

我很新使用vba中的类。我想使用数组作为一个属性,其中数组的长度必须是可变的。我四处寻找一种方法来做到这一点,但我真的不明白这些属性是如何工作的。redim在类模块中的属性

所以我定义我的数组类模块

Private pTestArray() As String 

和性质来获取和设置值

Private Property Get TestArrayValue(index As Long) As String 
    TestArrayValue = qTestArray(index) 
End Property 

Private Property Let ArrayValue(index As Long, strValue As String) 
    pTestArray(index) = strValue 
End Property 

,但我不能找到一种方法,REDIM阵列。任何线索? 谢谢 C

+0

请使用代码按钮'{}'来设置您的代码格式,并使您的帖子易读。 – Fionnuala 2012-03-13 10:30:52

回答

5

所以,你想调整任务?那么你可以检查&处理Let属性的界限;

Private Property Let ArrayValue(index As Long, strValue As String) 
    If index > UBound(pTestArray) Then ReDim Preserve pTestArray(index) 
    pTestArray(index) = strValue 
End Property 

您还需要在Class_Initialize事件与redim pTestArray(0)最初的尺寸。

+0

非常感谢Alex,那就是我一直在寻找的 – user1266138 2012-03-13 12:46:11

+0

+1好的解决方案,你也可以看看使用一个集合 – SWa 2012-03-14 16:19:13