Dispose, Dispose(true), Finalize, IDisposable

T

tascien

I am getting confused about the role of any of the above methods and
interfaced. But what I want to do is simple... the Destructor such as
the one in C#.

Basically, my only issue, is to make sure the Data Base connection
inside my class, DBCONN is closed, when the object is out of scope or
destroyed in either way.

In dotnet 1.1, the code below used to work. As the page finished
loading, or when db=Nothing, it should have written out:

Connection started
Connection Killed

but in dotnet 2.0, it doesn't. am I missing something here? I read the
information at:
http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx

but I do not want to completely take away the system's responsibility
to clean up. I just want to make sure the connection is closed. the
system should still have the responsibility to clean up.

In other words, I want a method that runs when the class is finished
or out of scope.


' ASPX code
--------------------------------------------
dim db = new DBCONN();
db = Nothing


' CLASS CODE
----------------------------------------------
Imports System.Data.SqlClient

Public Class DBCONN
Implements IDisposable

Private Conn As SqlConnection = Nothing

Sub New()

HttpContext.Current.Response.Write("Connection started")
Conn = New SqlConnection("ConnStr")

End Sub

Public Sub OpenConnection()
If Conn.State <> ConnectionState.Open Then
Conn.Open()
End If
End Sub

Public Sub CloseConnection()
If Conn.State <> ConnectionState.Closed Then
Conn.Close()
End If
End Sub

Sub Dispose() Implements System.IDisposable.Dispose

If Not Me.Conn Is Nothing Then

Me.CloseConnection()
Me.Conn = Nothing

End If

Web.HttpContext.Current.Response.Write("Connection Killed")

End Sub

End Class
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

I am getting confused about the role of any of the above methods and
interfaced. But what I want to do is simple... the Destructor such as
the one in C#.

You mean as in C++? There is no destructors in .NET, like the ones in C++.
Basically, my only issue, is to make sure the Data Base connection
inside my class, DBCONN is closed, when the object is out of scope or
destroyed in either way.

You can't do that. There is no reference counting in .NET, so there are
no destructors. There is no extra code executed when an object goes out
of scope, as there is in a system using reference counting, so there is
no way that you can make anything happen when an object goes out of scope.
In dotnet 1.1, the code below used to work. As the page finished
loading, or when db=Nothing, it should have written out:

Connection started
Connection Killed

No, it shouldn't. You have to explicitly call the Dispose method,
otherwise it will never be executed. There is nothing magic about the
IDisposable interface, it's just an interface as any other.
but in dotnet 2.0, it doesn't.

No, not there either.
am I missing something here? I read the
information at:
http://msdn2.microsoft.com/en-us/library/b1yfkh5e.aspx

but I do not want to completely take away the system's responsibility
to clean up.

You should not leave it to the system at all. You should call the
Dispose method so that you have control over when the connection is closed.

Better yet, use a Using statement. That creates a Try ... Finally
structure that ensures that the object is disposed whatever happens.
I just want to make sure the connection is closed. the
system should still have the responsibility to clean up.

The only way to make sure that the connection is closed is _not_ to
leave the clean up to the system.

Even if you implement a finalizer (which is common when you implement
IDisposable), you have no control over when the connection is going to
be closed. You don't even have a guarantee that it will ever be closed.
In other words, I want a method that runs when the class is finished
or out of scope.

There is no way of doing that. That's why there is an IDisposable interface.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top