访问隐藏的共享/静态变量

时间:2013-05-03 23:18:38

标签: .net scope

有了这个:

  class outer
    public shared X as string = ""
    class inner
      public shared sub test()
        Dim s as string
        s = X ' refers to the shared (static) variable in outer
      end sub
    end class
  end class

测试方法中对X的引用是对外部类中声明的共享变量,但是,如果我的内部类通过该名称声明变量(从外部类中删除共享变量),我该如何获取访问权限对吗?

  class outer
    public shared X as string = ""
    class inner
      public X as string = "x"
      public shared sub test()
        Dim s as string
        s = X ' this fails because it's an attempt to access an instance variable
      end sub
    end class
  end class

1 个答案:

答案 0 :(得分:1)

尝试指定外部类的名称,如下所示:

Class outer
    Public Shared X As String = "bar"

    Class inner
        Public X As String = "foo"
        Public Shared Sub test()
            Dim s As String = X             ' foo
            Dim t As String = outer.X       ' bar
        End Sub
    End Class
End Class
相关问题