私は VB6 のオブジェクト指向機能に取り掛かりました。私はJavaで多くのOOPを行ってきましたが、これを機能させようとしています:
オブジェクトの配列Card
があります。配列のインデックスにオブジェクトが作成されているかどうかを確認したいです。
Dim cardsPlayer1(1 To 10) As Card
次のようなオブジェクトを作成しました。
Set cardsPlayer1(index) = New Card
これを使用して、まだオブジェクトをインデックスに割り当てているかどうかをテストした場合:
For counter = 1 To 10
If (cardsPlayer1(counter) Is Nothing) Then
Set cardsPlayer1(counter) = New Card
End If
Next counter
しかし、それは毎回真の価値を与え、配列全体に新しいオブジェクトを作成しました。
関連するコードは次のとおりです。
'Jordan Mathewson
'May 31, 2013
Dim cardsPlayer1(1 To 10) As Card
Dim cardsPlayer2(1 To 10) As Card
Private Sub cmdStartGame_Click()
Call addCard(1)
End Sub
'Called to add a card to one of the player's stacks
Private Sub addCard(player As Integer)
Dim counter As Integer
'To add a card to player1..
If (player = 1) Then
For counter = 1 To 10
If (cardsPlayer1(counter) Is Nothing) Then
Print "Object created." '<- Printed 10 times.
Set cardsPlayer1(counter) = New Card
End If
Next counter
'To add a card to player2..
Else
For counter = 1 To 10
If (cardsPlayer2(counter) Is Nothing) Then
Set cardsPlayer2(counter) = New Card
End If
Next counter
End If
Call refreshForm
End Sub