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