Calling Dispose instead of close, clear, ....

  • Thread starter Thread starter Joop
  • Start date Start date
Lol, This function has actually made it into my "Automatically add to every
project right away" list, but I have it implemented a little bit
differently...

Public Sub DisposeObject(ByRef o As Object)
If Not IsNothing(o) Then
If GetType(IDisposable).IsAssignableFrom(o.GetType()) Then
CType(o, IDisposable).Dispose()
Else
If System.Runtime.InteropServices.Marshal.IsComObject(o) Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
End If
End If
o = Nothing
End If
End Sub

This 1> Sets the object referece = nothing, so that you don't have to worry
about accidently disposing it more than once (especially useful if you have
an object that you pass to another function, etc), and 2> will actually call
Release() on a COM object, decrementing the reference count to it immediatly
instead of waiting for the GC to get around to it. MS - Why don't you add a
Dispose to __ComObect to do this for us? It would make our code look alot
nicer...

Sean
 
Back
Top