问题
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