Dispose() Question

J

Jeremy

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
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (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.
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
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?
 
C

Cor Ligthert

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


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
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
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.
 
E

Eric Sabine

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
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
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 ;-).
 
C

Cor Ligthert

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
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
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.

;-)
 
J

Jay B. Harlow [MVP - Outlook]

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
 
C

Cor Ligthert

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
 
R

Ravichandran J.V.

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
 

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