Can I ReDim a module level array using a property?
I think I have a pretty good handle on how to handle module level arrays in VBA though Property Get and Let. Is there a way to ReDim a module level array through a property?
The following code errors out at the ReDim statement in the last procedure (DoTest).
Private mstrTestArray() As String
Private Sub Class_Initialize()
ReDim mstrTestArray(0) As String
End Sub
Private Property Get TestArray() As String()
TestArray = mstrTestArray
End Property
Private Property Let TestArray(ByRef strTestArray() As String)
mstrTestArray = strTestArray
End Property
Private Property Get TestArrayValue(d1 As Long) As String
TestArrayValue = mstrTestArray(d1)
End Property
Private Property Let TestArrayValue(d1 As Long, strValue As String)
mstrTestArray(d1) = strValue
End Property
Sub DoTest()
Dim intCharCode As Integer
For intCharCode = 97 To 122
If Not Len(TestArrayVal开发者_C百科ue(UBound(TestArray))) > 0 Then
TestArrayValue(UBound(TestArray)) = Chr(intCharCode)
Else
ReDim Preserve TestArray(UBound(TestArray) + 1) As String
TestArrayValue(UBound(TestArray)) = Chr(intCharCode)
End If
Next intCharCode
Debug.Print TestArrayValue(LBound(TestArray)) _
& " through " _
& TestArrayValue(UBound(TestArray))
End Sub
Thanks!
This is a good question. I'll answer your question directly at the bottom, but let's start with a brief background of Object-Oriented Programming in VBA. In most object-oriented languages, a property will often look like a field, but act like a method. What does this mean?
When you instantiate a class and set a value to a property, it looks like this:
Sub TestMyClass()
Dim mc As MyClass
Set mc = new MyClass
mc.MyProperty = 1
End Sub
In the above code, MyProperty
looks like a field, right? But let's look at how it's defined in the class:
Private pMyProperty As Integer
Public Property Get MyProperty() As Integer
MyProperty = pMyProperty
End Property
Public Property Let MyProperty(lMyProperty As Integer)
pMyProperty = lMyProperty
End Property
As you can see in the above code, while pMyProperty
is an Integer field, the public Get
and Set
methods for MyProperty
actually look more like methods. A property "wraps" around a field and is especially helpful in setting access to the underlying field.
In your example, you were trying to ReDim
a property that returns a reference to the array. I'm not 100% sure, but I don't think you can use ReDim
on a reference of an array.
I changed your code to modify the actual Private field mstrTestArray
and it seemed to work fine. Is that something you can try?
Sub DoTest()
Dim intCharCode As Integer
For intCharCode = 97 To 122
If Not Len(TestArrayValue(UBound(TestArray))) > 0 Then
TestArrayValue(UBound(TestArray)) = Chr(intCharCode)
Else
ReDim Preserve mstrTestArray(UBound(mstrTestArray) + 1) As String
TestArrayValue(UBound(TestArray)) = Chr(intCharCode)
End If
Next intCharCode
Debug.Print TestArrayValue(LBound(TestArray)) _
& " through " _
& TestArrayValue(UBound(TestArray))
End Sub
精彩评论