Hans Olsson
Hans Olsson

Reputation: 55019

Nested using statements

As Eric Gunnerson shows in this blog post, in C# you can nest using statements as:

using (StreamWriter w1 = File.CreateText("W1"))
using (StreamWriter w2 = File.CreateText("W2"))
{
    // code here
}

Is there a similar way to do it in VB.Net? I want to avoid too many indentation levels.

Upvotes: 31

Views: 5450

Answers (2)

SLaks
SLaks

Reputation: 887657

Like this:

Using a As New Thingy(), _
      b As New OtherThingy()
        ...
End Using

Upvotes: 45

Dan Tao
Dan Tao

Reputation: 128357

Well, you can do:

Using w1 = File.CreateText("W1"), w2 = File.CreateText("W2")
    ' Code goes here. '
End Using

Upvotes: 6

Related Questions