Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Related Posts
Laziness .NET -- the Professional Edition
Some new MSDN links of note
Visual Basic Resource Kit
Visual Basic Resource Kit Download Avail...
URLScan and the VB Resource Kit

'Using' statement added to VB in Whidbey release...

Posted on June 12th
Scott Watermasysk blogs about the new 'Using' statement in VB.NET 2005...
Using in VB.NET
I have not touched VB.NET since early in .NET Beta 1, so I am a bit rusty. One thing I was happy to find is support for using statements in VB.NET 2005. It took me a try or two to figure out the syntax, so I figured I would post it here for future reference.

Public Class Class1

    Public Sub Go()
        Using sw As StreamWriter = New StreamWriter("C:\hey.txt")
            sw.Write("HEY")
        End Using
    End Sub

End Class

via MSDN: "The using statement obtains one or more resources, executes a statement, and then disposes of the resource."

For those unfamailar with a using statement, you can use for classes which implement IDisposable. As soon as the variable defined in the using section goes out of scope, Dispose is called.

In VB.NET 2003 or 2002, you can get the same effect with this style of code
        Dim sw as New StreamWriter("C:\hey.txt")
        Try
            sw.Write("HEY")
        Finally
            sw.Dispose()
        End Try




Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

But shouldn't I still check for error-trapping... with either method?