2014-01-13 27 views
0

我一直在研究一個小型項目,我試圖通過VBA使用類模塊來獲得結果。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參數類型不匹配。

有什麼建議嗎?

回答

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),然後運行該代碼看什麼發生。

+0

感謝您的編輯,KazJaw。 –