从对象内部的对象访问对象的参数
Accessing an object's parameter from an object inside it
我正在编写一个程序,其中我在另一个 class 中有一个 class。我需要知道我是否可以从内部访问外部 class 的属性。
像这样:
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?
您仍然需要在这两个对象之间的某处创建一个 link。这是您如何操作的示例。
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
我正在编写一个程序,其中我在另一个 class 中有一个 class。我需要知道我是否可以从内部访问外部 class 的属性。
像这样:
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?
您仍然需要在这两个对象之间的某处创建一个 link。这是您如何操作的示例。
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