| |
8.22.2.IDisposable |
|
Class Point
Implements IDisposable
Public X = 3
Public Y = 4
Sub New(XX As Integer, YY As Integer)
X = XX
Y = YY
End Sub
Public Overrides Function ToString() As String
Return "(" & X & "," & Y & ")"
End Function
Public Overridable Overloads Sub Dispose() _
Implements IDisposable.Dispose
Console.WriteLine("Point " & Me.ToString() & " disposed of")
End Sub
End Class
Class TwoDimension
Implements IDisposable
Public First As Point
Public Second As Point
Public Sub New()
First = New Point(1, 2)
Second = New Point(3,4)
End Sub
Public Overrides Function ToString() As String
Return "(" & First.ToString() & "," & Second.ToString() & ")"
End Function
Public Overridable Overloads Sub Dispose() _
Implements IDisposable.Dispose
First.Dispose()
Second.Dispose()
First = Nothing
Second = Nothing
End Sub
Protected Overridable Overloads Sub Finalize()
First.Dispose()
Second.Dispose()
First = Nothing
Second = Nothing
End Sub
End Class
Module Test
Sub Main()
Dim P As TwoDimension = New TwoDimension()
Console.WriteLine("The object is: " & P.ToString())
P.Dispose()
P = Nothing
Console.WriteLine("The object, after disposal is " & P.ToString())
End Sub
End Module
|
|
The object is: ((1,2),(3,4))
Point (1,2) disposed of
Point (3,4) disposed of
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an ob
ject.
at Test.Main() |
|