Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top