VB.NET相当于{}嵌套

use*_*617 3 vb.net scope

VB.NET在C类语言中是否有任何范围嵌套运算符,如{}?

示例C++类型代码:


int i;
i = 0;
{
  int i;
  i++;
}

cha*_*rit 7

你可以使用一个空With:

With Nothing

    Dim x = 1

    Console.WriteLine("X = " + x.ToString())

End With

' ERROR! x is out of scope at this point. '
Console.WriteLine(x)
Run Code Online (Sandbox Code Playgroud)

因为With它只是一个语法糖,它可能比使用循环语句更好.


Jar*_*Par 5

不,VB.Net没有这样的毯子范围修饰符.您可以使用如下所示的空循环来模拟它们.

Loop
  ...
Until False
Run Code Online (Sandbox Code Playgroud)

但是,它仍然不允许您重新定义具有相同名称的变量.在VB.Net(和C#)中,在嵌套作用域中定义一个与外部作用域中的变量同名的变量是不合法的.