Monday, June 14, 2004

"Using" Statement in Whidbey

I just read here that the 'using' statement is going to be implemented in the next version of VB.NET.

I also read somewhere else that 'Using' is a really useful statement, and something that's been missing from VB for years (Apparently it's been about in the C variations for ages). What it does is this:

"The using statement obtains one or more resources, executes a statement, and then disposes of the resource."
This makes it pretty mich equivalent (apparently) to using a try..Catch..Finally block to ensure that Dispose() is called for any resources that implement it.

It might just be the example they chose, or it might just be me, but I can't see a huge amount of use for it - Using (pardon the pun) 'Using' ensures that resources are freed up when they're out of scope, but it doesn't have the error handling capability that a try..Catch..Finally has. If everything works fine, then that's OK, but if it doesn't, then the app'll fall over.

One rule of thumb I read (can't remember where, but it makes sense to me) is that every time you use a resource external to your app (Disk Files, Databases etc...) then that should all be wrapped in an error handling block. Kind of negates the need for 'Using' in my view...

Although having said that, I suppose it depends to a certain extent it depends how many resources you're using, and when the CLR actually disposes of them. Picture the scene:
Public Sub Go()
Using sw As StreamWriter = New StreamWriter("C:\hey.txt")
End Using
Catch ex as Exception
'Handle ex, notify user etc.
'Does anything need to go here?
End Try
End Sub
If the line sw.write("Hey") throws an exception, the execution steps out of the Using block and into the Catch block. Does the CLR dispose of sw at that point, or does it need a call to sw.Dispose in the finally section? Does it really add that much benefit? If it frees up resources regardless or not of whether the code within the Using block throws an exception or not, then it'll make the code more readable and certainly shorter, allowing you to do away with the Finally block altogether, but there are few examples that I can think of at the top of my head where this would be particularly desirable over a normal Try..Catch..Finally block.

I'm sure there are times when resources need to be disposed of, and yes, 'Using' does handle that for you, but in my (Ok, limited) experience, it doesn't seem to make a huge impact on the language. I'm sure that'll all change though, when I start 'Using' it (Boom Boom!).

Just a thought. Don't horsewhip me for being ungracious...


Anonymous said...

Thanks. That was helpful. The Microsoft documentation didn't lay it out so easily.

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Anonymous said...

I think it is two separate subjects.
The try catch can be used for a multitude of error handling situations whereas the Using would be used in cases where objects should be type casted.
With that I mean that you may have code that needs to process a number of different objects (derived from the same base class) which has the same objective, but different implementations. So, your variable would contain an object which should act as an instance of a specific class in certain scenarios.
Your code would then look like this:
Dim processForm As Form = Nothing
Select Case parentForm.Name
Case "MainForm"
Using processForm As frmMain
(process code here)
End Using
Case "AccountingForm"
Using processForm As frmAccounting
(process code here)
End Using
End Select