This post originated from an RSS feed registered with .NET Buzz
by Scott Watermasysk.
Original Post: Using in VB.NET
Feed Title: ScottW's ASP.NET WebLog
Feed URL: /error.aspx?aspxerrorpath=/blog/rss.aspx
Feed Description: ASP.NET and Blogging
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.