VB.Net/Language Basics/Finalize — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:42, 26 мая 2010

Finalize - called when the object is removed from memory

Imports System
Imports System.Drawing
Imports System.Data
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Drawing.Printing
Public Class MainClass
    Shared Sub Main()
      Dim o As MyObject = New MyObject
      
      o = Nothing
    End Sub
End Class

Public Class MyObject
    " Constructor - called when the object is started...
    Public Sub New()
        Console.WriteLine("Object " & GetHashCode() & " created.")
    End Sub
    " Finalize - called when the object is removed from memory...
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        " tell the user we"ve deleted...
        Console.WriteLine("Object " & GetHashCode() & " finalized.")
    End Sub
End Class


Finalize Class

Imports System.IO
Module Module1
    Sub Main()
        Dim c1 As New Contact("Name 1", "111-555-1111", "1@1.ru")
        Dim c2 As New Contact("Name 2", "222-555-1212", "2@2.ru")
        Dim c3 As New Contact("Name 3", "333-555-1212", "3@3.ru")
    End Sub
End Module
    Class Contact
        Public Name As String
        Public Phone As String
        Public EMail As String
        Sub New(ByVal ContactName As String, ByVal ContactPhone As String, ByVal ContactEmail As String)
            Console.WriteLine("Name: " & ContactName & " Phone " & ContactPhone & " Email " & ContactEmail)
            Name = ContactName
            Phone = ContactPhone
            EMail = ContactEmail
        End Sub
        Protected Overrides Sub Finalize()
            Console.WriteLine("In Finalize for " & Name)
        End Sub
    End Class


Finalize Objects

Imports System
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim new_numbered As New Numbered()
    End Sub
End Class
  
    Public Class Numbered
        Public Sub New()
        End Sub
        Protected Overrides Sub Finalize()
            Console.WriteLine("Garbage collection started " )
        End Sub
    End Class