crackedcornjimmy
crackedcornjimmy

Reputation: 1992

What is the VB.NET equivalent to C#'s 'using' block

I am coding in VB.NET.

Currently, I am enclosing object initialization and usage in a Try/Catch block and then tear it down (dispose, close, set to nothing) in the Finally block. The problem is, a C# 'using' block is so easy to use and would clean things up in my code.

Is there a VB.NET equivalent? Or is my Finally block tear-down solution the only way?

Upvotes: 28

Views: 19363

Answers (4)

AmbiguousX
AmbiguousX

Reputation: 1704

Here is another StackOverflow question that deals with the exact same issue. If I'm not mistaken it's used in a very similar, if not the exact same, way as in C# though.

Hope this helps!

Upvotes: 0

Rowland Shaw
Rowland Shaw

Reputation: 38130

Er, Using ... End Using

See MSDN for more info

Upvotes: 6

Hans Olsson
Hans Olsson

Reputation: 55009

It's the same, it's just:

Using conn As New SqlConnection
    ....
End Using

Upvotes: 48

Alex
Alex

Reputation: 35409

http://msdn.microsoft.com/en-us/library/htd05whh(VS.80).aspx

Public Sub setbigbold(ByVal c As Control)
    Using nf As New System.Drawing.Font("Arial", 12.0F, _
        System.Drawing.FontStyle.Bold)

        c.Font = nf
        c.Text = "This is 12-point Arial bold"
    End Using
End Sub

Upvotes: 7

Related Questions