我一直致力于一个小项目,我尝试通过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(来自单独的子模块)。
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参数类型不匹配。
有什么建议吗?
答案 0 :(得分:3)
此:
Public P1, P2 As TetrisPlayer
没有做你想象的那样。 P1现在是变体,P2是TetrisPlayer。相反,使用:
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)上设置断点,然后运行代码以观察发生的情况。