PC Review


Reply
Thread Tools Rate Thread

Dispose, Dispose(true), Finalize, IDisposable

 
 
tascien@gmail.com
Guest
Posts: n/a
 
      26th Jul 2007
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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      27th Jul 2007
(E-Mail Removed) wrote:
> 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.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dispose vs finalize Bhuwan Bhaskar Microsoft Dot NET Framework 2 31st Mar 2008 03:03 AM
the difference betweenSqlConnection.IDisposable.Dispose() andSqlConnection.Dispose(). tangyong Microsoft Dot NET Framework 1 20th Jan 2006 04:53 AM
the difference between SqlConnection.IDisposable.Dispose() and SqlConnection.Dispose() jinfeng_Wang@msn.com Microsoft Dot NET 1 18th Jan 2006 08:44 AM
IDisposable.Dispose, Object.Finalize and threads Francois PIETTE Microsoft Dot NET Framework 16 7th May 2004 06:42 PM
dispose vs finalize Joe Abou Jaoude Microsoft VB .NET 4 2nd Dec 2003 07:34 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:55 PM.