You can count how many objects have been instansized by putting code like this in a class module
' in code module for Class1
Public Name As String
Private Sub Class_Initialize()
With Range("A1")
.Value = Val(.Value) + 1
End With
End Sub
Private Sub Class_Terminate()
With Range("A1")
.Value = Val(.Value) - 1
End With
End Sub
Then when you run this code, I see that when an array of objects is delcared with the New keyword the elements of that array aren't instansized until the particular element is referenced.
Sub test()
Dim oArray(1 To 5) As New Class1
Range("A1").Value = 0: Rem intialize instance counter
Debug.Print Range("A1").Value & " start"
Debug.Print oArray(1) Is Nothing
Debug.Print Range("A1").Value & " a"
Erase oArray
Debug.Print Range("A1").Value & "b"
Debug.Print oArray(1) Is Nothing
Debug.Print Range("A1").Value & " c"
End Sub
This confusion is probably why it is considered best practice to not use New in declaration statements, but to explicitly instansize each object. In this case each element of the array.
Bookmarks