2009-12-17 92 views
2

在VBA类中处理模块级数组的正确方法是什么?在VBA类中处理模块级数组的正确方法是什么?

我使用Property LetProperty 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 

谢谢!

回答

2

下面的代码可能会给你一些帮助的线索。首先定义一个名为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 

然后在代码模块或工作表中创建一个名为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 

在类中,您可以更新strTestArray()Me.TestArrayValue。我会让你创建一个Get并为TestArray设置。我不是100%确定你要做什么..所以,如果你有任何问题请留言并更新你的OP :)

+0

这是我正在寻找的提示。你基本上必须为数组属性设置两组Let/Get。一个用于数组,另一个用于数组。我的代码中也有一个错字。谢谢马克! – Kuyenda 2009-12-17 05:50:07

+0

Mark,我在我的问题中添加了一些代码(在“以下不起作用”之后)。你可以请评论为什么不能从方法参数设置数组属性?是否因为这样做试图从ByRef创建一个ByRef?谢谢! – Kuyenda 2009-12-18 02:06:24

相关问题