19

In my VB6 application I have an array of objects declared thus...

Dim MyArray() as MyClass

This array is filled in as processing goes on

Set MyArray(element) = passed_object

and as elements are no longer wanted,

Set MyArray(otherelement) = Nothing

When using the array, I want to use a loop like

For i = 1 To Ubound(MyArray)
    If MyArray(i) <> Nothing Then    ' Doesn't compile
        ...do something...
    End If
Next i

But I can't get anything likely-looking to compile. I've also tried

If MyArray(i) Is Not Nothing Then

Should I want to do this, and if so, what test should I put in here?

Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
  • VB has an `IsNot` operator, so just remove a space to get `If MyArray(i) IsNot Nothing Then`... there is usually a preference for this syntax over `Not... Is Nothing` – u8it Jan 22 '16 at 16:10
  • 5
    @u8it, VB6 does not have an IsNot operator. That appeared only in VB.Net. – Luc VdV Sep 10 '18 at 10:11

4 Answers4

39
If Not MyArray(i) Is Nothing Then
mwolfe02
  • 23,787
  • 9
  • 91
  • 161
15
If Not MyArray(i) Is Nothing Then
AakashM
  • 62,551
  • 17
  • 151
  • 186
0

Instead of

IsNothing(<object here>)

this should work in VB6:

<object here> Is Nothing
0
    Private Function IsNothing(objParm As Object) As Boolean
        IsNothing = IIf(objParm Is Nothing, True, False)
    End Function
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49