Inspection Details

SelfAssignedDeclaration


Summary
Identifies auto-assigned object declarations.
Reasoning
Auto-assigned objects are automatically re-created as soon as they are referenced. It is therefore impossible to set one such reference to 'Nothing' and then verifying whether the object 'Is Nothing': it will never be. This behavior is potentially confusing and bug-prone.
The following code example(s) would trigger this inspection:

Public Sub DoSomething()
    Dim c As New Collection
    Set c = Nothing
    c.Add 42 ' no error 91 raised
    Debug.Print c.Count ' 1
    Set c = Nothing
    Debug.Print c Is Nothing ' False
End Sub

The following code example(s) would not trigger this inspection:

Public Sub DoSomething()
    Dim c As Collection
    Set c = New Collection
    Set c = Nothing
    c.Add 42 ' error 91
    Debug.Print c.Count ' error 91
    Set c = Nothing
    Debug.Print c Is Nothing ' True
End Sub


Back to List