【问题标题】:VBA: Class Modules and Arrays IssueVBA:类模块和数组问题
【发布时间】:2014-01-13 02:35:38
【问题描述】:

我一直在做一个小项目,我尝试通过 VBA 使用类模块来实现结果。

第一个问题:

以下语句来自类模块:

Private xRef As Integer
Private yRef As Integer
Private bValue As Boolean
Private NextTiles(1 To 4, 1 To 4) As Boolean

Public Property Get PreviewTiles(ByVal xRef As Integer, ByVal yRef As Integer) As Boolean
    PreviewTiles(xRef, yRef) = NextTiles(xRef, yRef)
End Property

Public Property Let PreviewTiles(ByVal xRef As Integer, ByVal yRef As Integer, ByVal bValue As Boolean)
    NextTiles(xRef, yRef) = bValue
End Property

在主子模块体中,存在如下语句:

Public P1, P2 As TetrisPlayer

Set P1 = New TetrisPlayer
Set P2 = New TetrisPlayer

...

P1.PreviewTiles(1, 1) = True
MsgBox P1.PreviewTiles(1, 1)

问题 1- 这返回说 P1.PreviewTiles(1,1) 的值应该为真时为假。

还有第二个问题:

以下代码基于一个单独的子模块,其中包含 P1 和 P2 的集合 Players(来自单独的子模块)。

Sub TETRIS_Start(FormName As String)

Dim Player As TetrisPlayer

For Each Player In Players
   Call TETRIS_GenerateShape(FormName, Player, True)
Next Player

End Sub

Sub TETRIS_GenerateShape(FormName As String, Player As TetrisPlayer, Start As Boolean)
...

这或多或少都很好(尽管遇到了问题 1)。所以我尝试使用以下语句进行调试:

Sub TETRIS_Start(FormName As String)

   Call TETRIS_GenerateShape(FormName, P1, True)

End Sub

问题 2 - 这导致对象 P1(公开声明,我什至尝试在本地声明)无法传递到子模块 TETRIS_GenerateShape。

出现的错误信息是: 编译错误:ByRef 参数类型不匹配。

有什么建议吗?

【问题讨论】:

    标签: arrays class vba object module


    【解决方案1】:

    这个:

    Public P1, P2 As TetrisPlayer
    

    没有做你认为的那样。 P1 现在是一个变体,P2 是一个俄罗斯方块播放器。相反,使用:

    Public P1 as TetrisPlayer, P2 as TetrisPlayer
    

    在 TetrisPlayer 或当前代码中使用它:

    Public Property Get PreviewTiles(ByVal xRef As Integer, ByVal yRef As Integer) As Boolean
        PreviewTiles = NextTiles(xRef, yRef)
    End Property
    
    Public Property Let PreviewTiles(ByVal xRef As Integer, ByVal yRef As Integer, ByVal bValue As Boolean)
        NextTiles(xRef, yRef) = bValue
    End Property
    

    首先,在 MsgBox P1.PreviewTiles(1, 1) 上设置断点,然后运行代码来观察会发生什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多