局部变量的块范围

pos*_*ner 5 vb.net scope

有没有更好的方法在VB.Net中创建任意块来限制局部变量的范围?如果尝试了"如果1然后",但它只是看起来像kludgy.

If 1 Then
    Dim table = InputParameter1
    Dim new_row = table.AddRow
    new_row.field(1) = InputParameter3.user_value
End If
Run Code Online (Sandbox Code Playgroud)

我只是不想在程序的后期使用tablenew_row访问.

Mar*_*urd 4

对于Option Strict On,那必须是If True Then,但更优选的选项(据我所知)是Do ... Loop While False

但是,不存在像简单的Begin ... End( 或{ ...}) 这样的语法。

如果您可以使用IDisposable, 并将其放置在块的末尾,Using newVariable = AnIDisposable那么您将获得最接近您所描述的内容,但就像我说的,它被放置在末尾。