Imports System
Imports Microsoft.VisualBasic
Module MyModule
Sub Main()
Dim object1 As New MyClass1(1)
object1. CheckDisposal ()
object1.Dispose()
object1.CheckDisposal()
object1.Dispose()
Dim object2 As New MyClass1(2)
End Sub
End Module
Class MyClass1
Implements IDisposable
Private name As String
Private disposed As Boolean
Public Sub New(ByVal n As String)
name = n
disposed = False
Console.WriteLine("Constructor for {0}", name)
End Sub
Public Sub CheckDisposal()
If Not disposed
Console.WriteLine("{0} still in use", name)
End If
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
If Not disposed Then
Console.WriteLine("Dispose for {0}" & vbCrLf, name)
disposed = True
GC.SuppressFinalize(Me)
End If
End Sub
Protected Overrides Sub Finalize()
Console.WriteLine("Destructor for {0}", name)
Dispose()
End Sub
End Class
Constructor for 1
1 still in use
Dispose for 1
Constructor for 2
Destructor for 2
Dispose for 2