473,390 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 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 5587
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Reply-Via-Newsgroup | last post by:
Folks, I have a table containing addresses. I have a seperate contacts table that contains peoples names - This allows me to record more than one persons name related to a single address. My...
5
by: Si | last post by:
Hi All. This may be DB issue, but rather that cross post, I'll post here first. I have a page on a site that submits a form to search for various different values in a database. It's a...
6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
3
by: Adam | last post by:
Hello, I have a 2 column file that looks like this: VALUE1 | VALUE2 12 | 7 10 | 7 9 | 8 10 | 8 11 | 8 6 | 9 8 | 9
7
by: Tinus | last post by:
Hello all, I was wondering... Is there a way to create a Array so that I can call a value with a name i.s.o. a index number? Something similar like a DataTable. This is what I want: Declare a...
59
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
3
by: chrismcb | last post by:
In C++ I want to statically create an array that contains variable arrays inside of it. Or something similar. Essentially I want to do something like this: { {"name1", {a, b, c} }, {"name2",...
2
by: sajanmp | last post by:
I am learning C#. I hav a few questions. Can anyone help me out? Question 1: I would like to know is there is any similar statement to the statement starting with WITH.. END WITH in VB.NET. For...
6
by: Tem | last post by:
I have a single table that contains information of photos ie. ID PhotoName PhotoTags 23 my cat cat animal pet 24 cell phone electronic communication 25 tiger animal zoo...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.