473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Dispose() Question

I was looking through some sample code that Microsoft provided for data access
and I noticed that when they call the Dispose() method on the Connection
object they cast it to IDisposable first.

CType(connection, IDisposable).Dispose()

Is there any reason that you couldn't just do:
connection.Dispose() ?

What is the difference?

TIA
Nov 20 '05 #1
16 1871
Good programming practice/readability...
"Jeremy" <jm**********@yahoo.com> wrote in message
news:11**************************@posting.google.c om...
I was looking through some sample code that Microsoft provided for data access and I noticed that when they call the Dispose() method on the Connection
object they cast it to IDisposable first.

CType(connection, IDisposable).Dispose()

Is there any reason that you couldn't just do:
connection.Dispose() ?

What is the difference?

TIA

Nov 20 '05 #2
* jm**********@yahoo.com (Jeremy) scripsit:
I was looking through some sample code that Microsoft provided for data access
and I noticed that when they call the Dispose() method on the Connection
object they cast it to IDisposable first.

CType(connection, IDisposable).Dispose()

Is there any reason that you couldn't just do:
connection.Dispose() ?


No, remove the 'CType' stuff.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Hi Jeremy,

When you see some graphic encoder samples it goes like this (not exact)

Dim myImage as Image();

I think the same reason.

Cor
Nov 20 '05 #4
* "Cor Ligthert" <no**********@planet.nl> scripsit:
When you see some graphic encoder samples it goes like this (not exact)

Dim myImage as Image();
That's a bad translation, the editor forgot to remove the semicolon.
I think the same reason.


Do you think the editor replaces a simple call to the 'Dispose' method
in the C# version to a 'CType' + 'Dispose' call in the VB.NET version?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
> > Dim myImage as Image();

That's a bad translation, the editor forgot to remove the semicolon.
I think the same reason.


Do you think the editor replaces a simple call to the 'Dispose' method
in the C# version to a 'CType' + 'Dispose' call in the VB.NET version?


No I think this is fast converted as more things on MSDN.
Some documentation is great (and more and more) however some is bad.

Cor
Nov 20 '05 #6
* "Cor Ligthert" <no**********@planet.nl> scripsit:
Do you think the editor replaces a simple call to the 'Dispose' method
in the C# version to a 'CType' + 'Dispose' call in the VB.NET version?
No I think this is fast converted as more things on MSDN.


What do you mean by "fast converted"?
Some documentation is great (and more and more) however some is bad.


ACK. Would be great if the OP would post the URL to the documentation
page with this code.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7
Do you have any microsoft application blocks installed on your machine? if
so in my Data Access Application Block v2 source code for
"DataAccessQuickStartSamples", look in all of the Finally blocks of Form1.

hth
Eric


"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:c6************@ID-208219.news.uni-berlin.de...
* "Cor Ligthert" <no**********@planet.nl> scripsit:
Do you think the editor replaces a simple call to the 'Dispose' method
in the C# version to a 'CType' + 'Dispose' call in the VB.NET version?


No I think this is fast converted as more things on MSDN.


What do you mean by "fast converted"?
Some documentation is great (and more and more) however some is bad.


ACK. Would be great if the OP would post the URL to the documentation
page with this code.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #8
Hi Eric,

The programmers from Microsoft are also only human, just like you and me.

And as far as I know are there no completly perfect humans.

Cor
Nov 20 '05 #9
* "Cor Ligthert" <no**********@planet.nl> scripsit:
The programmers from Microsoft are also only human, just like you and me.

And as far as I know are there no completly perfect humans.


I don't know many people who type too much code ;-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #10
Hi Herfried,

I just ended a long discussion in the adonet newsgroup.

About the sense from this
object.dispose
object = nothing

That was told as being good practise.

Cor
Nov 20 '05 #11
* "Cor Ligthert" <no**********@planet.nl> scripsit:
I just ended a long discussion in the adonet newsgroup.

About the sense from this
object.dispose
That's good pratice.
object = nothing
If this is useful depends on where the code is placed. In general,
that's not good practice.
That was told as being good practise.


;-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #12
> > About the sense from this
object.dispose


That's good pratice.


That "can be" good practise.

Cor
Nov 20 '05 #13
Jeremy,
In addition to the other's comments.

The biggest reason to cast to the interface first, is that the Dispose
method can be implemented as either hidden (private) or another name on the
object. Which also means that Dispose may not do the same thing as
IDisposable.Dispose! Note: I would need to seriously consider the
ramifications of having a Dispose method on a class that did not do
IDisposable.Dispose, however its possible that someday I need to do that.
;-)

For example:

Public Class MyConnection
Implements IDisposable

Public Sub Close() Implements IDisposable.Dispose
End Sub

End Class

Dim connection As MyConnection
connection.Dispose() ' syntax error!
DirectCast(connection, IDisposeable).Dispose() ' works

I've noticed the above in some of the Windows Forms classes, or where a
class implements an interface, such as IList in CollectionBase, and the code
really wants to call IList.Add (instead of the type safe Add in the derived
class).
It just happens that OdbcConnection, OldeDbConnection, and SqlConnection all
inherit Dispose from Component, which is the IDisposable.Dispose method. So
for a connection object, its not that important to cast first.

Hope this helps
Jay
"Jeremy" <jm**********@yahoo.com> wrote in message
news:11**************************@posting.google.c om...
I was looking through some sample code that Microsoft provided for data access and I noticed that when they call the Dispose() method on the Connection
object they cast it to IDisposable first.

CType(connection, IDisposable).Dispose()

Is there any reason that you couldn't just do:
connection.Dispose() ?

What is the difference?

TIA

Nov 20 '05 #14
* "Cor Ligthert" <no**********@planet.nl> scripsit:
About the sense from this
object.dispose


That's good pratice.


That "can be" good practise.


It /is/ in almost every case if the object implements 'IDisposable'.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #15
It is saterday night,

You know what that means

However I will be glad to see tomorrow why I should use in every object that
derives from MarshalByValueComponent "Dispose" just because that is a
derived member, and why I should not use everytime Equals, GethHashCode,
GetService, Gettype etc. and moreover always gethashcode which is even
derived from an higher class.

(You know of course that I have probably my answer on your answer already
ready)

:-)

Cor
Nov 20 '05 #16
A Dispose method can be called on an object only if it implements an
IDisposable interface and hence, by casting it to IDisposable type, the
programmer is ensuring that he does not get a Interface IDisposable not
implemented for object or some such thing.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
4
by: xyu | last post by:
Hello, First I would like to thank anyone who helps me, much appreciated. I'm a c++ programmer just started using c#. I'm writing some big hash table so I want to actively take my object off...
15
by: Sam Sungshik Kong | last post by:
Hello! A disposable object's Dispose() method can be called either explicitly by the programmer or implicitly during finalization. If you call Dispose, the unmanaged resources are released...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
13
by: Grafix | last post by:
All - As we all know, Dispose method is reserved in C++ 8 and the expected syntax is to use ~MyClass(). In 1.1, we used to have following structure for Dispose class MyClass : IDisposable {...
1
by: jinfeng_Wang | last post by:
hi, I have a question about the difference between SqlConnection.IDisposable.Dispose() and SqlConnection.Dispose(). Both of them realize the function of releasing the connection to the...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
3
by: AlexS | last post by:
When I implement Dispose pattern in object implementing IDisposable, current fxcop recommends: Ensure that Wrapper.Dispose():Void is declared as public and sealed. However, if I do as it asks,...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.