Hi Jay,
Many thanks for your improvement. It still works as long as both methods
contain the disposing code - then the new method gets called most of the
time, and the older less efficient method gets called for the remainder. So
the new improved Disposer class now looks like this:
Class Disposer
Public Shared Sub Dispose(ByVal DisposableObject As Object)
If Not DisposableObject Is Nothing Then
If TypeOf DisposableObject Is IDisposable Then
DirectCast(DisposableObject, IDisposable).Dispose()
End If
End If
End Sub
Public Shared Sub Dispose(ByVal DisposableObject As IDisposable)
If Not DisposableObject Is Nothing Then
DisposableObject.Dispose()
End If
End Sub
End Class
Regards,
Mark
--
Author of "Comprehensive VB .NET Debugging"
http://www.apress.com/book/bookDisplay.html?bID=128
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:#d**************@TK2MSFTNGP12.phx.gbl...
Mark,
Of course my overloaded function assumes your variables are the correct
type, if you had a disposable object in an variable of type Object, or a
type that does not implement IDisposable then the routine would not call
Dispose.
I knew there was a caveat, I just clicked send too soon...
Jay
"Mark Pearce" <ev**@bay.com> wrote in message
news:u7**************@TK2MSFTNGP10.phx.gbl...
Hi Andreas,
While still not as good as the "using" statement, you can create a helper
class like this:
Class Disposer
Public Shared Sub Dispose(ByVal DisposableObject As Object)
If Not DisposableObject Is Nothing Then
If TypeOf DisposableObject Is IDisposable Then
DirectCast(DisposableObject , IDisposable).Dispose()
End If
End If
End Sub
End Class
You can then use the helper class like this:
Dim con As New
SqlConnection("server=localhost;database=northwind ;integrated security=sspi")
Dim xact As SqlTransaction = Nothing
Dim writer As StringWriter = Nothing
Try
con.Open()
xact = con.BeginTransaction()
writer = New StringWriter(CultureInfo.CurrentCulture)
'Do some work
Finally
Disposer.Dispose(con)
Disposer.Dispose(xact)
Disposer.Dispose(writer)
End Try
HTH,
Mark
--
Author of "Comprehensive VB .NET Debugging"
http://www.apress.com/book/bookDisplay.html?bID=128
"Andreas Müller" <an***************@gmx.de> wrote in message
news:#z*************@TK2MSFTNGP12.phx.gbl...
Hi,
I was wondering, if there is something similar in VB.NET like the using
statement in C#. What it does is to automatically call Dispose on the
object decrared with in the statement when the block exits.
using (MyBoj)
{
}// MyObj.Dispose is called
Thanks in advance,
Andy