Object lifetime : Object « Class Module « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Class Module » Object 
6.36.2.Object lifetime
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
6.36.Object
6.36.1.Assign string value to an Object variable
6.36.2.Object lifetime
6.36.3.Reference Equality
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.