访问嵌套类中的共享父级字段/属性 [英] Accessing shared parent fields/properties in nested classes

查看:86
本文介绍了访问嵌套类中的共享父级字段/属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有

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

然后我不能使用MyBase,因为它不是派生的,并且由于共享了Test,所以我无法将父级的实例传递给Inner的构造函数...我也不能将其称为Outer.Index,因为Outer不会在对Inner进行编译时还不存在,当然,在简单的引用中,所引用的字段将是Inner中定义的字段...所以我该怎么做?

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?

推荐答案

重新阅读您的问题后,我删除了之前的答案.

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

我刚刚在VS2010中测试了以下课程:

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()

执行此代码时,立即"窗口中将打印"1".

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

这篇关于访问嵌套类中的共享父级字段/属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆