4

Okay, so I'm upgrading a VB6 app to VB.NET and I'm unsure of how to modernize the class_terminate component of a container class I'm building:

Private Sub class_terminate()
    If Not (colUserMappings Is Nothing) Then
        Set colUserMappings = Nothing
    End If
End Sub

The issue is that the .NET equivalent of this .Finalize leaves open some potential runtime errors, because setting a container's final reference to nothing doesn't necessarily destroy the container, as .NET languages have non-deterministic finalization.

That being the case, how would I modernize the collections class in such a way that calling its terminate or finalize function would actually result in the destruction of the container at the end? Is there a good workaround for this?

MarkJ
  • 30,070
  • 5
  • 68
  • 111
Adam Boyce
  • 98
  • 1
  • 6

1 Answers1

7

I would not care too much about this. .Net is a managed, garbage-collected environment. I'm pretty sure the CLR will take care of collecting these objects for you when they're no longer needed.

As a general rule, you should only worry about these kind of things when you're getting a hold of unmanaged resources (such as files, DB Connections, COM objects, etc.). In that case, you may want to implement IDisposable and properly release all your objects / resources in the Dispose() method.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • It will be adding user access credentials from a DB Connection, but the container class itself isn't going to be managing the connections. So the CLR should handle it then? – Adam Boyce Jun 11 '13 at 17:33
  • @VK_Dev yes. for normal objects and classes, don't worry about setting them to `null`. .Net will handle that for you. – Federico Berasategui Jun 11 '13 at 17:34