What is the proper way to handle a module level array in a VBA class?
I use Property Let
and Property Get
for other variables, but I haven't figured out how to pass arrays to and from Properties.
Updated. This code works thanks to Mark Nold.
Option Explicit
Private mstrTestArray() As String
Public Property Get TestArray() As String()
TestArray = mstrTestArray
End Property
Public Property Let TestArray(ByRef strTestArray() As String)
mstrTestArray = strTestArray
End Property
Public Property Get TestArrayValue(d1 As Long, d2 As Long) As String
TestArrayValue = mstrTestArray(d1, d2)
End Property
Public Property Let TestArrayValue(d1 As Long, d2 As Long, strValue As String)
strTestArray(d1, d2) = strValue
End Property
Sub DoTest()
Dim strTestArray() As String
ReDim strTestArray(2, 1) As String
strTestArray(0, 0) = "a": strTestArray(0, 1) = "one"
strTestArray(1, 0) = "b": strTestArray(1, 1) = "two"
strTestArray(2, 0) = "c": strTestArray(2, 1) = "three"
TestArray = strTestArray
Debug.Print TestArrayValue(UBound(TestArray, 1), UBound(TestArray, 2))
End Sub
The following does not work. This top part is a method in the above class:
Sub LetArrayFromReference(ByRef strTestArray() As String)
TestArray = strTestArray
End Sub
This part is a procedure that calls the class:
Sub DoTest()
Dim strTestArray() As String
ReDim strTestArray(2, 1) As String
Dim objTestClass As New TestClass
strTestArray(0, 0) = "a": strTestArray(0, 1) = "one"
strTestArray(1, 0) = "b": strTestArray(1, 1) = "two"
strTestArray(2, 0) = "c": strTestArray(2, 1) = "three"
objTestClass.LetArrayFromReference strTestArray
Debug.Print objTestClass.TestArrayValue(UBound(objTestClass.TestArray, 1) _
, UBound(objTestClass.TestArray, 2))
End Sub
Thanks!