467,119 Members | 1,180 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,119 developers. It's quick & easy.

Using statements and returns

If I have a function and inside it have something like this
Using Obj1
Using Obj2
Return Value
End Using
End using

The Using statements still know how to handle the dispose and cleanup
correctly dont they? Or do I have to put the return outside of the using
blocks... I was always under the impression you can return inside of them
and it will still dispose as needed... thanks!
Aug 14 '07 #1
  • viewed: 1399
Share:
4 Replies
Yes - "Using" is implemented via a Try/Finally block (and Dispose call)
behind the scenes and will handle this perfectly well.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, and C++
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
"Smokey Grindel" wrote:
If I have a function and inside it have something like this
Using Obj1
Using Obj2
Return Value
End Using
End using

The Using statements still know how to handle the dispose and cleanup
correctly dont they? Or do I have to put the return outside of the using
blocks... I was always under the impression you can return inside of them
and it will still dispose as needed... thanks!
Aug 14 '07 #2
On Aug 14, 12:05 pm, "Smokey Grindel" <nos...@nospam.comwrote:
If I have a function and inside it have something like this

Using Obj1
Using Obj2
Return Value
End Using
End using

The Using statements still know how to handle the dispose and cleanup
correctly dont they? Or do I have to put the return outside of the using
blocks... I was always under the impression you can return inside of them
and it will still dispose as needed... thanks!
Also note you can do this:

Using Obj1, Obj2
Return Value
End Using

I know it's not the answer to your question (as David beat me to
it :-)), I just thought you might like to know if you didn't already.

Thanks,

Seth Rowe

Aug 14 '07 #3
"Smokey Grindel" <no****@nospam.comschrieb:
If I have a function and inside it have something like this
Using Obj1
Using Obj2
Return Value
End Using
End using

The Using statements still know how to handle the dispose and cleanup
correctly dont they?
Yes.

The code equivalent to those the compiler generates for each 'Using' block
basically looks like that:

\\\
Dim Obj As IDisposable
Try
<Contents of the 'Using' block>
Catch
Finally
Obj.Dispose()
End Try
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Aug 14 '07 #4
Errata:
Dim Obj As IDisposable
.... should read 'Dim Obj As <Type implementing 'IDisposable'= ...'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Aug 14 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by sureshjayaram@gmail.com | last post: by
39 posts views Thread by slogging_away | last post: by
12 posts views Thread by rAinDeEr | last post: by
1 post views Thread by MackTheKnife | last post: by
9 posts views Thread by John | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.