我有Class1和class2,它在class1,VB.NET代码中: Public Class class1 Public varisbleX As Integer = 1 Public Class class2 Public Sub New() 'Here GET the value of VariableX End Sub End Class Public Sub New() Dim cls2 As New class2 End Su
Public Class class1 Public varisbleX As Integer = 1 Public Class class2 Public Sub New() 'Here GET the value of VariableX End Sub End Class Public Sub New() Dim cls2 As New class2 End Sub End Class
我想从class2访问varisbleX,在VB.net或C#中的代码表示赞赏,谢谢.
内部类(class2)不与外部类(class1)的任何特定实例相关联.在T访问字段等中,您需要首先对class1实例进行显式引用,可能通过构造函数传递它.例如,它可能是:Public Class class1 Public varisbleX As Integer = 1 Public Class class2 Public Property Parent As class1 Public Sub New(oParent As class1) Me.Parent = oParent Console.WriteLine(oParent.varisbleX) End Sub End Class Public Sub New() Dim cls2 As New class2(Me) End Sub End Class