Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of declaring a Class within another Class?

I come from the VBA world where options to breakdown your code into classes, namespaces, and modules is limited. Now I just landed in a world where the options are many, and I feel lost.

I would like to know what is the purpose of declaring a Class within another Class? (see example below)

Class FirstClass
    Public OnePropertyInside As String
    Class SecondClass
        Public AnotherProperty As String
    End Class
End Class

If I create a new instance of FirstClass (say myFirstClass), SecondClass is not instantiated. Even more bizzare (to me at least), is that intelissense offers me myFirstClass.SecondClass. Obviously, because the class is not instantiated, I cannot access any of its members.


So, is that usefull only if the SecondClass contains shared members? To try answering that question I added a shared member within SecondClass:

Class FirstClass
    Public OnePropertyInside As String
    Class SecondClass
        Public AnotherProperty As String
        Public Shared SharedProperty As String
    End Class
End Class

I ran a few tests which brought secondary questions (see comments in code)

Sub Main()       

    Dim myFirstClass As New FirstClass
    'Works as expected
    Console.WriteLine(myFirstClass.OneProperty)

    'What is the difference between the two lines below?
    Console.WriteLine(myFirstClass.SecondClass.SharedProperty)
    Console.WriteLine(FirstClass.SecondClass.SharedProperty)

    'This line cannot be compiled, this demonstrates SecondClass is not instantiated when FirstClass is.
    Console.WriteLine(myFirstClass.SecondClass.AnotherProperty)

    Dim mySecondClass As New FirstClass.SecondClass
    'Works as expected, but I feel this hierarchy should better be dealt with through a namespace statement?
    Console.WriteLine(mySecondClass.AnotherProperty)

End Sub
like image 492
Ama Avatar asked Jan 02 '23 07:01

Ama


2 Answers

You can think of it as if the inner most class is a helper class of sorts. It may not even need to be used at all. Nesting the inner class(or simply nested class) inside the outer class gives you access to all of the members of the outer one. You can even access the private members inside that initial outer class.

Edit: For clarification, I mean to say that the the inner can access the private members of the outer, not the other way around.

like image 170
AMDarwech Avatar answered Jan 03 '23 21:01

AMDarwech


You usually do this because you want to restrict the scope of the nested class.

So, if you only need to use this class from within the "parent" class (in terms of scope), then its usually a good idea to define it as a nested class.

If you might might need to use the class outside of its assembly, then it is better to define it as a completely separate class (in its own file), and then define your relationship accordingly. You will need to instantiate one within the other (this is the same whether its seperate or nested - so its location is largely irrelevant for that point).

like image 43
Robert Perry Avatar answered Jan 03 '23 21:01

Robert Perry