Search code examples
vb6

How to check if a value of an array is empty


I am writing a chess program and has to see whether certain value are empty. I tried

aPiece is an array of CommandButtons. cmdSquare is a control array of shapes.

Private aPiece(63) As CommandButton

...

For p = 0 To 63
    If IsEmpty(aPiece(p)) Then
        aPiece(p).Left = cmdSquare(p).Left
        aPiece(p).Top = cmdSquare(p).Top
    
    End If
Next p 

All variable are declared and is seem to be IsEmpty function which is not working.


Solution

  • The IsEmpty method only returns meaningful information for variants. Since the array contains objects, you need to check like this:

    If aPiece(p) Is Nothing Then
    

    However, this seems like only part of the answer. The above logic is saying "If there is no piece in my array then update it's location". That doesn't make sense to me and will generate an error. You also need to add Not like below:

    For p = 0 To 1
       If Not aPiece(p) Is Nothing Then
          aPiece(p).Left = cmdSquare(p).Left
          aPiece(p).Top = cmdSquare(p).Top
       End If
    Next p