我一直在研究 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