2013-05-03 42 views
2

利用该:访问隐藏的共享/静态变量

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 
+0

内部类具有比其访问私处没有特殊关系外,其他。它将需要对外部对象的引用。 – 2013-05-04 00:09:41

回答

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 
+0

我曾试过,但似乎无法得到正确的语法。编译器会抱怨:“错误:'外部'没有声明。它可能无法访问,由于其保护级别”但我不明白为什么... – ekkis 2013-05-03 23:27:05

+0

@ekkis我刚试过它,并能够得到它的工作没有任何问题。也许'outer'需要是公开的。 – 2013-05-03 23:35:07