【发布时间】:2010-12-27 12:31:38
【问题描述】:
在 VBA 类中处理模块级数组的正确方法是什么?
我将Property Let 和Property Get 用于其他变量,但我还没有弄清楚如何将数组传入和传出属性。
已更新。感谢 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
以下不起作用。这个顶部是上面类中的一个方法:
Sub LetArrayFromReference(ByRef strTestArray() As String)
TestArray = strTestArray
End Sub
这部分是调用类的过程:
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
谢谢!
【问题讨论】:
标签: arrays vba class properties