Private nested classes

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
+5
source share
3 answers

, / . , . - :

Module EntryPoint
    Sub Main()
        Dim OuterClassInstance As New OuterClass
        OuterClassInstance.NestedClassInstance.GetParentX()
    End Sub
End Module

Public Class OuterClass
    Public X As Integer = 123
    Public NestedClassInstance As ISomeImplementation = New NestedClass(Me)

    Private Class NestedClass
        Implements ISomeImplementation
        Private Parent As OuterClass

        Public Sub New(ByVal _Parent As OuterClass)
            Parent = _Parent
        End Sub

        Public Sub GetParentX() Implements ISomeImplementation.GetParentX
            Debug.WriteLine("X = " & Parent.X.ToString)
        End Sub
    End Class
End Class

Public Interface ISomeImplementation
    Sub GetParentX()
End Interface
+6

, .

, , .

, ( ) , .

+3

, friend. , .

0

All Articles