Accessing shared parent fields/properties in nested classes
Asked Answered
R

1

0

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?

Riverine answered 25/7, 2012 at 19:2 Comment(0)
A
0

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.

Ageold answered 25/7, 2012 at 19:12 Comment(2)
if I were to declare Inner.Index as Shared then it becomes a simple issue of shadowing, so my question is basically: how do I overcome the shadow? most languages provide some mechanism to refer to an outer context...Riverine
Sorry, re-read your message. I missed the original Shared Index and was focused on the inner index instead. Please see my revised answer.Ageold

© 2022 - 2024 — McMap. All rights reserved.