31

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116

2 Answers2

45

Like this:

Using a As New Thingy(), _
      b As New OtherThingy()
        ...
End Using
Ry-
  • 218,210
  • 55
  • 464
  • 476
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
6

Well, you can do:

Using w1 = File.CreateText("W1"), w2 = File.CreateText("W2")
    ' Code goes here. '
End Using
Dan Tao
  • 125,917
  • 54
  • 300
  • 447