【问题标题】:Cant add custom class to an array无法将自定义类添加到数组
【发布时间】:2019-09-04 15:54:01
【问题描述】:

我有一个名为 PLAYER 的自定义类,它具有以下属性以及关联的 getter 和 setter 以及默认 init。

Private aName As String
Private aAge As Integer
Private aID As String
Private aScore As Integer

我现在尝试在主程序中创建一个数组,如下所示:


    Dim Players As Range
    Set Players = Range("PLAYERS")

    Dim PlayerRows As Integer, PlayerCols As Integer, count As Integer

    PlayerRows = Players.Rows.count

    Dim allPlayers() As PLAYER
    ReDim allPlayers(5)

    Dim temp As New PLAYER
    temp.name = Players.Cells(count + 1, 1)
    temp.age = Players.Cells(count + 1, 2)
    temp.ID = Players.Cells(count + 1, 3)
    temp.score = Players.Cells(count + 1, 4)

    allPlayers(0) = temp

但是当我尝试将新对象添加到 PLAYER 对象数组时,我得到一个运行时错误 91 '对象变量或未设置块变量'。

我如何设置这个数组错误?我正在尝试为 PLAYER 类创建一个数组。

【问题讨论】:

    标签: arrays vba class


    【解决方案1】:

    处理必须使用“Set”关键字的对象:

    Set allPlayers(0) = temp
    

    【讨论】:

    • 感谢您的快速反馈。如果我要这样做并尝试将玩家添加到列表中,它看起来将最后一个玩家添加到列表中的索引甚至索引:` Do While (endLoop = False) Dim temp As New PLAYER temp.name = Players .Cells(count + 1, 1) If ((count + 1) = PlayerRows) Then endLoop = True End If Set allPlayers(count) = temp count = count + 1 Loop••••ˇˇˇ `
    • 发布一个新问题
    猜你喜欢
    • 2014-08-29
    • 1970-01-01
    • 2017-12-08
    • 2011-01-10
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    相关资源
    最近更新 更多