【问题标题】:What is the proper way to handle module level arrays in a VBA class?在 VBA 类中处理模块级数组的正确方法是什么?
【发布时间】:2010-12-27 12:31:38
【问题描述】:

在 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

谢谢!

【问题讨论】:

    标签: arrays vba class properties


    【解决方案1】:

    下面的代码可能会为您提供一些帮助线索。首先定义一个名为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。我会让你为 TestArray 创建一个 Get 和 set。我不能 100% 确定您要做什么..所以如果您有任何问题,请发表评论并更新您的 OP :)

    【讨论】:

    • 这就是我正在寻找的提示。对于数组属性,您基本上必须有两组 Let/Get。一个用于值,一个用于数组。我的代码中也有错字。谢谢马克!
    • 马克,我在我的问题中添加了一些代码(在“以下内容不起作用”之后)。您能否评论一下为什么不能从方法参数设置数组属性?是因为这样做试图从 ByRef 创建一个 ByRef 吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    相关资源
    最近更新 更多