472,119 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

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

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
5 5477
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
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
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
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
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.

Similar topics

4 posts views Thread by Reply-Via-Newsgroup | last post: by
6 posts views Thread by Michael Sparks | last post: by
3 posts views Thread by Adam | last post: by
7 posts views Thread by Tinus | last post: by
6 posts views Thread by Tem | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.