Accessing shared parent fields/properties in nested classes

Deadly 提交于 2019-12-11 17:18:26

问题


suppose we have:

Class Outer
   Public Shared Index As Integer
   Class Inner
      Private Index As Integer
      Public Shared Sub Test()
         ' how do I refer to the parent's Index?
      End Sub
   End Class
End Class

then I can't use MyBase because it's not derived and I can't pass the instance of the parent to Inner's constructor because Test is shared... I also cannot refer to it as Outer.Index because Outer doesn't yet exist at the time Inner is getting compiled, and of course, in a simple reference the referenced field would be that defined in Inner... so how do I do it?


回答1:


After re-reading your question, I have removed my previous answer.

I just tested the following class in VS2010:

Class Outer
    Public Shared Index As Integer
    Class Inner
        Private Index As Integer
        Public Shared Sub Test()
            Debug.WriteLine(Outer.Index)
        End Sub
    End Class
End Class

I then added the following code to test:

    Outer.Index = 1
    Outer.Inner.Test()

When I execute this code, "1" is printed in the Immediate window.



来源:https://stackoverflow.com/questions/11656743/accessing-shared-parent-fields-properties-in-nested-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!