laughing chocolates
laughing chocolates

Reputation: 185

Nested class Access methods for Properties in .NET

I am trying to figure out the best approach for setting and getting properties in a nested class I am creating.

I have a class, Car which has a nested class ControlPanel and want to make the properties of the Control Panel only accessible to the Car and Control Panel class.

(ie: not within the assembly or namespace and not within the application the class library will be going to be used)... I have changed the class access properties to friend, protected friend, private, public, but any combination is not matching my expected results.

I want to change the properties in the Drive() Sub of a class as shown below.

Any thoughts?

 Public Class Car

    Dim cp As New ControlPanel

    Public Class ControlPanel
      Private _Speedometer As Integer = 0
      Private _Odometer As Integer = 0

      Public Property Speedometer() As Integer
        Get
            Return _Speedometer
        End Get
        Protected Set(ByVal value As Integer)
            _Speedometer = value
        End Set
      End Property

      Public Property Odometer() As Integer
        Get
            Return _Odometer
        End Get
        Protected Set(ByVal value As Integer)
            _Odometer = value
        End Set
     End Property

    End Class

   Public Sub Drive()

        cp.Odometer = 76323
        co.Speedometer = 86

   End Sub

End Class

Upvotes: 8

Views: 6974

Answers (5)

Jordão
Jordão

Reputation: 56537

You can do it like this:

Public Class Car

  Private Interface IControlPanel
    Property Odometer As Integer
    Property Speedometer As Integer
  End Interface

  Public Class ControlPanel
    Implements IControlPanel
    Public ReadOnly Property Odometer As Integer
      Get
        Return CType(Me, IControlPanel).Odometer
      End Get
    End Property
    Public ReadOnly Property Speedometer As Integer
      Get
        Return CType(Me, IControlPanel).Speedometer
      End Get
    End Property
    Private Property IControlPanel_Odometer As Integer Implements IControlPanel.Odometer
    Private Property IControlPanel_Speedometer As Integer Implements IControlPanel.Speedometer
  End Class

  Dim cp As IControlPanel = New ControlPanel()

  Public Sub Drive()
    cp.Odometer = 76323
    cp.Speedometer = 86 
  End Sub

End Class

It uses a private interface nested in the Car class with privately implemented and aliased members in ControlPanel. This way, only Car can access the interface members.

Upvotes: 3

Dustin Davis
Dustin Davis

Reputation: 14605

nested classes is one way to go or you can have your Car as a composite class. Control panel can be used in many different cars (sub classes) so why make it nested? Car has a Control Panel. Car has an Engine

Bus : Car also has those things.

Radio might be something that Control Panel has. Speedometer could be part of Dashboard that can also be part of the Control Panel.

Then you end up with

MyCar.ItsControlPanel.Radio MyCar.ItsControlPanel.Dashboard.Speedometer.CurrentSpeed;

How about Motorcycle? It isnt a car but it still has a Dashboard which has a Speedometer.

Get my point? Composite objects.

Upvotes: 0

Chris Baxter
Chris Baxter

Reputation: 16373

As Robert Levy pointed out, you are referring to a "Nested Class" and not a "Subclass" etc.

As for how to achieve what you are looking for... are you simply looking to make ControlPanel a private class? That will ensure that all members of ControlPanel are only accessible to Car. If you have other members on ControlPanel that need to be exposed, or the outside outside world needs to hold a reference to ControlPanel in someway, consider using an Interface to expose only those members that you want to be publically available.

Public Class Car

  Dim cp As New ControlPanel

  Private Class ControlPanel

    Public Property Speedometer As Integer
    Public Property Odometer As Integer

  End Class

  Public Sub Drive()
    cp.Odometer = 76323
    cp.Speedometer = 86
  End Sub

End Class

Optionally...

Friend Interface IControlPanel
  //Whatever actually needs to be publically accessible.    
End Interface

// Other Code...

Private Class ControlPanel
  Implements IControlPanel

// Other Code...

What is the goal that you are trying to achieve in terms of API?

Upvotes: 4

Mark Simpson
Mark Simpson

Reputation: 23365

Make your ControlPanel class private and its properties public. The ControlPanel class will then only be visible to the Car type, but Car will still be able to modify ControlPanel's properties.

Upvotes: 0

Robert Levy
Robert Levy

Reputation: 29083

First, let's clarify terminology. What you are talking about here is a "nested" class and not a subclass

I dont believe what you are trying to do is possible in .NET. However, do you really need it? If cp is private within Car, nobody can increment the odometer on that car's control panel. They could create their own controlpanel that isn't part of a car and then mess with it, but what's the harm in that?

Upvotes: 0

Related Questions