Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 310
laughing chocolates Avatar asked Jan 20 '11 02:01

laughing chocolates


People also ask

Can nested classes access private members C#?

A nested type has access to all of the members that are accessible to its containing type. It can access private and protected members of the containing type, including any inherited protected members.

Which access modifiers we can give to nested class?

A nested class can be declared with any access modifier, namely private, public, protected, internal, protected internal, or private protected.

What are the properties of static nested class?

Java static nested class A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name. It can access static data members of the outer class, including private.


2 Answers

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?

like image 199
Chris Baxter Avatar answered Oct 19 '22 23:10

Chris Baxter


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.

like image 26
Jordão Avatar answered Oct 20 '22 01:10

Jordão