【问题标题】:Excel vba array of objects with matrix带有矩阵的Excel vba对象数组
【发布时间】:2015-04-09 12:19:17
【问题描述】:

我正在尝试创建一个对象数组。所有对象都来自同一个类。 我希望每个对象都有一个矩阵(二维数组)。但我做不到。

我的模块 1 中有这个:

Public Arr As Variant

clsStudent 类是:

Public ID As Integer
Public Name As String
Public Teacher As String
Private Matrix(6, 14) As String 

这是在我的 Sheet1 子中:

Dim i, k As Integer
i = 20
ReDim Arr(0 To (i)) As clsStudent ' array with students size
For k = 0 To i
    Set Arr(k) = New clsStudent
Next k
Arr(0).ID = 123
Arr(0).Matrix(0, 0).Value = "123"  'error here

最后一行给了我一个错误:

对象不支持该属性或方法

我尝试将矩阵更改为:

Public Matrix(6,14) as String

但我收到此错误:

常量、定长字符串、数组、用户定义类型和声明语句不允许作为对象模块的公共成员

我该如何解决这个问题?

【问题讨论】:

    标签: arrays vba excel


    【解决方案1】:

    您可以尝试创建属性来访问您的类实例中的私有 Matrix 字段。属性允许您定义私有类字段的公共接口。保持矩阵私有,但给它一个唯一的名称,例如:

    Private pMatrix(6, 14) As String 
    

    唯一的名称允许您在 GetLet 属性中使用更直观的名称。这些属性允许您分别定义读取和写入接口,可能如下所示:

    Public Property Let Matrix(i As Integer, j As Integer, value As String)
        pMatrix(i, j) = value
    End Property
    
    Public Property Get Matrix(i As Integer, j As Integer) As String
        Matrix = pMatrix(i, j)
    End Property
    

    您可以将它们直接放在类模块中的字段声明下方。

    最后,您可以从工作表子例程中删除.Value 方法,pMatrix 字段不包含此方法。 = 告诉编译器您希望调用 Let 属性,它将两个属性参数 (0, 0) 作为属性 ij 中的前两个参数以及右侧的值赋值为第三个参数value

    我修改后的代码全部列在下面:

    ****clsStudent****
    Option Explicit
    
    Public ID As Integer
    Public Name As String
    Public Teacher As String
    Private pMatrix(6, 14) As String
    
    Public Property Let Matrix(i As Integer, j As Integer, value As String)
        pMatrix(i, j) = value
    End Property
    
    Public Property Get Matrix(i As Integer, j As Integer) As String
        Matrix = pMatrix(i, j)
    End Property
    
    ****clsStudent****
    
    ****Module1****
    Option Explicit
    
    Public Arr As Variant
    ****Module1****
    
    ****Sheet1****
    Option Explicit
    
    Sub test()
    
    Dim i As Integer, k As Integer
    i = 20
    
    ReDim Arr(0 To (i)) As clsStudent ' array with students size
    
    For k = 0 To i
        Set Arr(k) = New clsStudent
    Next k
    
    Arr(0).ID = 123
    Arr(0).Matrix(0, 0) = "123"  'no error here anymore
    
    Debug.Print Arr(0).Matrix(0, 0)
    
    End Sub
    ****Sheet1****
    

    对于信息,Module1 代码不是必需的。

    ***已编辑以包含 shA.t 关于变量声明的评论

    【讨论】:

      【解决方案2】:

      建议您使用以下方法,而不是使用公共二维数组:

      Public Function getMatrix(Bound1 As Integer, Bound2 As Integer) As String
          getMatrix = Matrix(Bound1, Bound2)
      END Function
      

      Public Sub setMatrix(Bound1 As Integer, Bound2 As Integer, mValue As String)
          Matrix(Bound1, Bound2) = mValue
      END Function
      

      注解:
      Dim i, k As Integer 等同于Dim i As Variant, k As Integer,但不等同于Dim i As Integer, k As Integer

      使用Dim i As Integer, k As Integer

      【讨论】:

      • 多亏了你,我想我快到了。我错过了一件事,在我的工作表中,我相信我现在应该输入:Arr(0).setmatrix(0,0,"123") 但它说的是预期 =
      猜你喜欢
      • 2019-01-27
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 2019-01-13
      相关资源
      最近更新 更多