次の方法で共有


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

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

Comments

  • Anonymous
    June 12, 2004
    I think most of us are numb to Whidbey. We are just trying to make a living when what we can use today.

    This tip included a sample for VB.NET 2003 ... Thanks ! We would like to see more.

    Thanks
  • Anonymous
    June 12, 2004
    The comment has been removed
  • Anonymous
    June 12, 2004
    "Applying Whidbey concepts in Everette" posts are great. Posts like that help us all prepare for Whidbey more than anything.

    Features are cool, mindset is key.

  • Anonymous
    June 14, 2004
    I LOVE DELPHI
  • Anonymous
    June 18, 2004
    Uhm, AFAIK, you can also get the same functionality by dropping the Try, Finally, and End Try keywords...
  • Anonymous
    June 18, 2004
    The comment has been removed