By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,167 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,167 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Nope.

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

Please respond to the newsgroup,
so all can benefit

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

Nov 20 '05 #2

P: n/a
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
Nov 20 '05 #3

P: n/a
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! End Sub 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 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

Nov 20 '05 #4

P: n/a
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

Nov 20 '05 #5

P: n/a
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


Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.