Is there something similar in VB.NET like the using statement in C#

  • Thread starter Thread starter Andreas =?ISO-8859-1?Q?M=FCller?=
  • Start date Start date
A

Andreas =?ISO-8859-1?Q?M=FCller?=

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
 
Nope.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
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


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
 
Mark,
Consider overloading the function instead of using TypeOf:
Class Disposer
Public Shared Sub Dispose(ByVal DisposableObject As Object)
' DisposableObject does not implement IDisposable
' nothing to do!
Public Shared Sub Dispose(ByVal DisposableObject As IDisposable)
If Not DisposableObject Is Nothing Then
DisposableObject.Dispose()
End If
End Sub
End Class

If the passed object implements IDisposable,then the second routine will be
called, and the Dispose method will be called.

If the object does not implement IDisposable, then the first routine will be
called, and there is nothing to do.

Hope this helps
Jay
 
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
 
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


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
 
Back
Top