What is the proper way to handle module level arrays in a VBA class?
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开发者_StackOverflow中文版()
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!
The code below might give you some clues to help. First define a class called TestClass
.
Option Explicit
Private strTestArray() As String
Public Property Get TestArrayValue(d1 As Long, d2 As Long) As String
TestArrayValue = strTestArray(d1, d2)
End Property
Public Property Let TestArrayValue(d1 As Long, d2 As Long, sValue As String)
strTestArray(d1, d2) = sValue
End Property
Sub DoTest()
Dim myTestArray() As String
ReDim myTestArray(3, 1) As String
myTestArray(0, 0) = "a": myTestArray(0, 1) = "one"
myTestArray(1, 0) = "b": myTestArray(1, 1) = "two"
myTestArray(2, 0) = "c": myTestArray(2, 1) = "three"
strTestArray = myTestArray
Me.TestArrayValue(3, 1) = "Hello"
Debug.Print strTestArray(2, 1)
Debug.Print Me.TestArrayValue(1, 1)
Debug.Print Me.TestArrayValue(3, 1)
End Sub
Then in a code module or a worksheet create a sub called MyTest();
Option Explicit
Sub MyTest()
Dim t As New TestClass
t.DoTest
Debug.Print "The value at 1,1 is; " & t.TestArrayValue(1, 1)
End Sub
Within the class you can update strTestArray()
or Me.TestArrayValue
. I'll let you create a Get and set for TestArray. I'm not 100% sure of what you are trying to do..so if you have any questions leave a comment and update your OP :)
精彩评论