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_
via MSDN: “The using statement obtains one or more resources, executes a statement, and then disposes of the resource."
For those unfamiliar 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
Thoughts on this post? Feel free to reach out on Twitter!
2004-06-13 04:40 +0000
a74df3b @ 2019-04-17