VBA: Difference in two ways of declaring a new object? (Trying to understand why my solution works)

Fink’s answer gets your main problem right, which is that your first loop is adding multiple references to the same instance of ‘clsMatch’ to your collection. I’ll just elaborate on why your fix works.

In VBA, a line like:

Dim c As New Collection

doesn’t actually create a new collection. The ‘Dim’ statement is always just a declaration. Think of the ‘As New’ form as being shorthand for this:

Dim c As Collection
'...

'(later, when you're about to use 'c')

If c Is Nothing Then
    Set c = New Collection
End If

'...

That is why destroying your reference by setting the variable that contained it to ‘Nothing’ was working. [NOTE: to whomever edited this to say “was not” – that changes the meaning of the answer and makes it incorrect. Please read the original question. The OP found that setting the variable to Nothing did work, and I was explaing why that was the case.] When the loop came back around to the ‘oMatch.setLineNumber’ line, VBA “helpfully” created a new instance of ‘clsMatch’ for your ‘oMatch’ variable to refer to, and then you got multiple different instances in your collection.

It would probably be better to do this explicitly:

Dim oMatch As clsMatch   

For i = 0 To 10                
    Set oMatch = New clsMatch                
    oMatch.setLineNumber i                
    oCollection.Add oMatch                
Next  

Note that (unlike in C/C++ or ??.NET) it doesn’t matter where the ‘Dim’ declaration goes. It’s not being “executed” multiple times inside the loop, and the scope of what it declares is procedure-wide even though it appears inside the loop.

Leave a Comment