I want to create a nested class that can only be visible and created from the parent class. But I also want to be able to use an instance of a nested class through the public variable of the parent class.
I tried to make the nested class private or to make my own constructor of the nested class private, but it will not compile. Is it possible to do this in .NET?
This compiles and works, but the nested class can be used by someone:
Public Class OuterClass
Public X As Integer = 123
Public NestedClassInstance As New NestedClass(Me)
Public Class NestedClass
Private Parent As OuterClass
Public Sub New(ByVal _Parent As OuterClass)
Parent = _Parent
End Sub
Public Sub GetParentX()
Debug.WriteLine("X = " & Parent.X.ToString)
End Sub
End Class
End Class
Sub Main()
Dim OuterClassInstance As New OuterClass
OuterClassInstance.NestedClassInstance.GetParentX()
End Sub
source
share