0

I have been getting into some object-oriented features of VB6. I've done lots of OOP with Java, and I'm trying to get this to work:

I have a array of Card objects, I want to check if an object in the index in the array has been created.

Dim cardsPlayer1(1 To 10) As Card

I created objects like this:

Set cardsPlayer1(index) = New Card

If tried using this to test if I have assigned an object to an index yet:

For counter = 1 To 10
    If (cardsPlayer1(counter) Is Nothing) Then
        Set cardsPlayer1(counter) = New Card
    End If
Next counter

But it gave me a true value everytime and creating a new object to the whole array.

Here's the relevant code:

'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
6
  • 2
    That's correct. I can't see why that wouldn't work. Is there some other part of this you're not showing? Or how do you know it's doing it wrong? Commented Jun 4, 2013 at 13:55
  • 1
    Can you copy and paste code rather than retyping? specifically the first two samples, It may help to uncover typo bugs. Commented Jun 4, 2013 at 14:47
  • An index can't be empty, though an element can be. Commented Jun 4, 2013 at 14:50
  • Alright, I'll show all the code tomorrow when I can get back to my computer. Commented Jun 4, 2013 at 19:14
  • Updated it with the actual code. Commented Jun 5, 2013 at 14:02

2 Answers 2

1

If I understand you correctly, the addCard sub should add one card but it adds all of them, when only called once. This isn't because it can't tell which array element is empty. It's just because it doesn't stop after successfully adding one.

For counter = 1 To 10
    If (cardsPlayer1(counter) Is Nothing) Then
        Set cardsPlayer1(counter) = New Card
        Exit For ' <-- Add this
    End If
Next counter

Without the Exit For, it will keep looping through the array and initialize the rest of it.

Sign up to request clarification or add additional context in comments.

1 Comment

That's it. I cant believe I missed that. Thanks!
0

I suspect you might have a scoping issue. This gives me the expected results:

Sub Test()
    Dim objectsArray(1 To 5) As TestObject

    If objectsArray(1) Is Nothing Then
        MsgBox "objectsArray(1) Is Nothing"    ' <----- displayed
    End If

    Set objectsArray(1) = New TestObject

    If objectsArray(1) Is Nothing Then
        MsgBox "objectsArray(1) Is Nothing"
    Else
        MsgBox "Not objectsArray(1) Is Nothing"    ' <----- displayed
    End If
End Sub

Where do you declare objectsArray; where do you create the object; where is the loop? (Are these code snippets in different modules/class modules/functions?)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.