Nested classes or something ?

ddebono

Member
Joined
Nov 25, 2013
Messages
6
Programming Experience
5-10
Hi!
I want to build up a nested object model that don't expose the lower classes directly to the caller.

For example if the top object is Car

If want to call it like: Car.Seat.Color=2, or Car.Door.Left.Lock

I don't want the class Seat/Door to be directly visible to the the caller, but only through the Car object.

I also want the Seat/Door class to be in a separate class file for easier update, and not the whole code inserted into the Car code.

I looked at implementing it using a Property in the Car object (worked in VB6), but the Seat class must then be visible (Public) otherwise it reports an error.

Of course this example is just made up, but it's easier to understand then :)
 
You can accomplish what you want through the Partial class declaration keyword:

Partial Public Class Car
    Public Property OneSeat As Seat
    Public Property OneDoor As Door
End Class

Partial Public Class Car
    Public Class Seat
        Public Property Color As Integer
    End Class

    Public Class Door
        Public Sub Lock()
        End Sub
    End Class
End Class

        Dim c As Car
        Dim d As Car.Door
        Dim s As Car.Seat


Keep in mind however separating class definitions like this is not recommended, as it makes it hard to keep track of all the partial definitions. To achieve what you want this would be the only way however, as you need Seat and Door to be part of Car, but you ALSO need Seat and Door to be in separate source files. Be careful how you name each file, and make sure you explain it all properly in comments.
 
Despite what Herman says, what you are asking for is not possible. Even in Herman's example, you can still create an instance of the Seat or Door class; you simply have to qualify the type name when you do. The simple fact is that, if you want a type to be exposed via public property or method then that type has to be public. Either it can be seen or it can't. You can't have it both ways.

What you can do is ensure that, even though a type can be seen, an instance can't be created. An example of this is the DataRow class. You can access DataRow objects from a DataTable and assign them to variables of your own but you cannot create a DataRow object directly. Only a DataTable can do that. The way that is achieved is by declaring all constructors Friend or less, e.g.
Public Class Car

    Public Sub New()
        Seat = New Seat()
        Door = New Door()
    End Sub

    Public Property Seat() As Seat

    Public Property Door() As Door

End Class


Public Class Seat

    Friend Sub New()
    End Sub

    Public Property Color() As Integer

End Class


Public Class Door

    Friend Sub New()
    End Sub

    Public Sub Lock()
        '...
    End Sub

End Class
The Car class can create instances of the Seat and Door types because it is defined in the same assembly but code outside that assembly cannot see the constructors declared Friend so cannot create instances.
 
I think I understand now. What I'm trying to accomplish is impossible.
I can however stop the sub classes from being created outside Car by setting the constructor as Protected or Protected Friend, but I cannot hide the Seat and Door objects.
Am I correct ?
 
I think I understand now. What I'm trying to accomplish is impossible.
I can however stop the sub classes from being created outside Car by setting the constructor as Protected or Protected Friend, but I cannot hide the Seat and Door objects.
Am I correct ?

Almost correct. If you declare the constructor Protected then the Car class will not be able to create instances either, which I don't think you want. Protected means that only the class itself or classes that inherit it can access the constructor. Friend means that only code in the same project/assembly can access it.
 
Back
Top