在.NET中,没有析构函数!!!
所有的对象在系统都会根据具体情况自动回收。对于特定的对象,你也可以自行在某些方法中通过DISPOSE方法或CLOSE方法删除或关闭。

解决方案 »

  1.   

    老兄如果我在一个类的构造函数NEW()中打开了一个sqlconnection,,当析构的时候我要关闭连接,该怎么做那,多谢了
      

  2.   

    c#中就是
    ~类名()
    vb7查了一下MSDN:
    构造函数和析构函数控制对象的创建和毁坏。 若要为类创建构造函数,请在类定义的任何位置创建名为 Sub New 的过程。若要创建参数化构造函数,请像为其他任何过程指定参数那样为 Sub New 指定参数的名称和数据类型,如下面的代码所示:Sub New(ByVal sString As String)
    构造函数频繁地重载,如下面的代码所示:Sub New(ByVal sString As String, iInt As Integer)当定义从另一个类派生的类时,构造函数的第一行必须是对基类构造函数的调用,除非基类有一个可访问的无参数构造函数。例如,对包含以上构造函数的基类的调用将为 MyBase.New(sString)。另外,MyBase.New 是可选的,Visual Basic .NET 运行库会隐式调用它。在编写调用父对象的构造函数的代码后,可以向 Sub New 过程添加任何附加初始化代码。当 Sub New 作为参数化构造函数调用时可以接受参数。这些参数从调用构造函数的过程传递(例如 Dim AnObject As New ThisClass(X))。下面的代码实例展示如何使用 Dispose 和 Finalize 释放资源:Class BaseType
        Implements IDisposable
        Public Sub Dispose() Implements IDisposable.Dispose
    ' Place cleanup code here.
        End Sub 
    End ClassClass ResourceWrapper
        Inherits BaseType
        Implements IDisposable    Private handle As IntPtr ' Pointer to an external resource.
        Private otherRes As OtherResource ' Other resource you happen to use.
        Private disposed As Boolean = False    Public Sub New()
          handle = ' Resource allocated by call to unmanaged API.
          otherRes = New OtherResource() ' Object that wraps a resource.
        End Sub     ' Free your own state.
        Private Sub freeState()
            If Not disposed Then
                CloseHandle(handle)
                disposed = True
            End If
        End Sub     ' Free your own state, call Dispose on all held states, and leave
        ' the finalization queue.
        Public Overrides Sub Dispose() Implements IDisposable.Dispose
            freeState()
            otherRes.Dispose()
            MyBase.Dispose() ' If base type implements Dispose, call it.
            ' Calling GC.SuppressFinalize in Dispose is an important
            ' optimization because it ensures that resources are 
            ' released promptly.
            GC.SuppressFinalize(Me)
        End Sub     ' Free your own state (NOT other state you hold) and give the
        ' parent class a chance to finalize. 
        Overloads Overrides Sub Finalize()
            freeState()
            MyBase.Finalize()
        End Sub     ' Whenever you do something with this class, check to see if the   
        ' state is disposed. If so, throw this exception.
        Public Sub SimpleDoStuff()
            If disposed Then
                Throw ObjectDisposedException("ResWrapper")
            End If
        End Sub 
    End Class 
    下面的示例说明使用 Dispose 析构函数的一个通用设计模式:Dim con As Connection, rs As RecordSet
    Try
       con = New Connection("xyz")
       rs = New RecordSet(con, "MyTable")
       ' Use the connection if successful.
    Finally
    ' Call the Dispose method when done with any created objects.
    If Not con Is Nothing Then
          con.Dispose()
       End If
       If Not rs Is Nothing Then
          rs.Dispose()
       End If
    End Try
    下一个示例使用参数化构造函数创建一个对象,并在不再需要该对象时调用析构函数:Sub TestConstructorsAndDestructors()
       Dim X As Integer = 6
       Dim AnObject As New ThisClass(X)
       ' Place statements here that use the object.
       AnObject.DoSomething()
       ' Test the parameterized constructor. 
       MsgBox("The value of ThisProperty after being initialized " & _
              " by the constructor is " & AnObject.ThisProperty)
       ' Run the Dispose method when you are done with the object.
       AnObject.Dispose()
    End Sub   Public Class BaseClass
          Sub New()
             MsgBox("The base class is initializing with Sub New.")
          End Sub      Protected Overrides Sub Finalize()
             MsgBox("The base class is destroying objects with Sub Finalize.")
             ' Place final cleanup tasks here.
             MyBase.Finalize()
          End Sub
       End Class   Public Class ThisClass
          Inherits BaseClass
          Implements IDisposable ' Implements the Dispose method of IDisposable.
          Private m_PropertyValue  As Integer      Sub New(ByVal SomeValue As Integer)
             MyBase.New() ' Call MyBase.New if this is a derived class.
             MsgBox("Sub New is initializing the class ThisClass.")
             ' Place initialization statements here. 
             m_PropertyValue  = SomeValue
          End Sub      Property ThisProperty() As Integer
             Get
                ThisProperty = m_PropertyValue 
             End Get
             Set(ByVal Value As Integer)
                m_PropertyValue  = Value
             End Set
          End Property      Sub DoSomething()
             ' Place code here that does some task.
          End Sub      Protected Overrides Sub Finalize()
             MsgBox("Finalize is destroying objects in ThisClass.")
             ' Place final cleanup tasks here.
             MyBase.Finalize()
          End Sub      Overridable Sub Dispose() Implements IDisposable.Dispose
             MsgBox("The ThisClass is running Dispose.")
          End Sub
       End Class
    当运行此示例时,ThisClass 类调用 BaseClass 类的 Sub New 构造函数。在基类中的构造函数完成以后,ThisClass 类运行 Sub New 中剩余的语句,这些语句初始化 ThisProperty 属性的值。当不再需要该类时,在 ThisClass 中调用 Dispose 析构函数。如果最初是从窗体创建 ThisClass 的实例,则在关闭该窗体之前似乎什么都没有发生。Finalize 析构函数此时在 ThisClass 类中运行,最后将在 BaseClass 类中运行。