Share via


Using Using According to Proper Usage

Have you ever been in a conversation or meeting when terms are being used that you somehow sense you should already know? Happens all of the time. Well, let me help every one out here by telling a big secret: chances are you are not the only one in the conversation or meeting that hasn't a clue what's pulling off. So, relax your shoulders, loosen the tension in your face, smile pleasantly and start asking questions.

I had one of these moments recently about the Using keyword added to VB.NET in 2005. I feel like I should have picked up on this one a lot sooner, especially because it erases a ton of issues for most solutions: tinkering with .NET's memory management.

It used to be that once you would New up an object, you either left the clean up to .NET or you went full-contact and implemented the IDisposable interface and called Dispose() etc. all on your own. This is no fun, and it makes the code messy. Usually, I would try my best to hide all of that stuff away, but questions always arose. This is not necessarily bad, but when you are on a tight timeline with a lot to do, it's a drag re-hashing issues. Inevitably, discussions about the GC lead to sweeping pontifications by someone about proper memory management, weaknesses in this or that, blah, blah, blah. Bottom line for me is: I need to get stuff done.

Well, the Using keyword in VB.NET causes an object that implements IDisposable to call Dispose at the end of the Using block. Cool. Here's an example:

Dim myCoolObject As New CoolObject

Using (myCoolObject)

   'Do whatever you are going to do with this object

End Using

You probably knew that, but in case you didn't. There's the 411. You can find out more in this online book chapter: Language Enhancements for Visual Basic 2005.

Rock Thought for the Day: The RHCP new album, "Stadium Arcadium" comes out on Tuesday (I'll have my copy that day). You can see the video for their kickin single, "Dani California" here.

Rock On