从对象内部的对象访问对象的参数

时间:2015-08-04 07:26:03

标签: .net vb.net basic

我编写了一个程序,我在另一个类中有一个类。我需要知道我是否可以从内部类访问外部类的属性。

这样的事情:

Module mod1

    Public Class c1
        Public var1 As Integer = 3

        Public Class c2

            Public Sub doSomething()
                'I need to access var1 from here. Is it possible?
            End Sub

        End Class

    End Class

End Module

非常感谢您的帮助!

编辑:我想做的例子

Dim obj1 As New c1 'Let's suppose that the object is properly initialized
Dim obj2 As New obj1.c2 'Let's suppose that the object is properly initialized

obj2.doSomething() 'Here, I want to affect ONLY the var1 of obj1. Would that be possible?

1 个答案:

答案 0 :(得分:1)

您仍然需要在某处创建这两个对象之间的链接。这是一个如何做到这一点的例子。

Dim obj1 As New c1
Dim obj2 As New c2(obj1)

obj2.doSomething()

doSomething 现在可以影响 c1 c2 中定义的变量。实现:

Public Class c1
    Public var1 As Integer = 3
End Class

Public Class c2
    Private linkedC1 As c1

    Public Sub New(ByVal linkedC1 As c1)
        Me.linkedC1 = linkedC1
    End Sub

    Public Sub doSomething()
        'I need to access var1 from here. Is it possible?
        linkedC1.var1 += 1
    End Sub

End Class