user3188739
user3188739

Reputation: 3

VBA: Class Modules and Arrays Issue

I've been working on a small project in which I attempted to use class modules through VBA to achieve results.

First Question:

The following statements are from the class module:

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

In the main submodule body, the following statement exists:

Public P1, P2 As TetrisPlayer

Set P1 = New TetrisPlayer
Set P2 = New TetrisPlayer

...

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

Problem 1- This returns saying that the value of P1.PreviewTiles(1,1) False when it should be true.

Also second question:

The following code below is based on a seperate submodule, with the collection Players which includes P1 and P2 (from a separate submodule).

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)
...

This works more-or-less fine (although it encounters problem 1). So I tried to debug with the following statement instead:

Sub TETRIS_Start(FormName As String)

   Call TETRIS_GenerateShape(FormName, P1, True)

End Sub

Problem 2 - This results in the object P1 (publically declared, I even tried to declare it locally) not being able to pass through to the submodule TETRIS_GenerateShape.

The error message that arises is: Compile Error: ByRef argument type mismatch.

Any suggestions?

Upvotes: 0

Views: 275

Answers (1)

Steve Rindsberg
Steve Rindsberg

Reputation: 14809

This:

Public P1, P2 As TetrisPlayer

isn't doing what you think it is. P1 is now a variant, P2 is a TetrisPlayer. Instead, use:

Public P1 as TetrisPlayer, P2 as TetrisPlayer

Use this in TetrisPlayer instead or the current code:

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

First, set a breakpoint on MsgBox P1.PreviewTiles(1, 1) then run the code to watch what happens.

Upvotes: 3

Related Questions