Nested classes Access methods for properties in .NET.

I am trying to find a better approach to set and get properties in a nested class that I create.

I have a Car class that has a nested ControlPanel class and wants the control panel properties to be accessible only to the Car and Control Panel class.

(i.e. not inside the assembly or namespace, and not inside the application, the class library will be used) ... I changed the class’s access properties to a friend, protected by a friend, private, public, but any combination does not match my expected results.

I want to change the properties in the Sub () Sub 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 
+8
access-modifiers
source share
5 answers

You can do it as follows:

 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 a privately implemented and smoothed member in the ControlPanel . Thus, only Car can access the interface elements.

+3
source share

As Robert Levy remarked, you mean "nested class", not "subclass", etc.

As for how to achieve what you are looking for ... do you just want to make the ControlPanel a private class? This ensures that all members of the ControlPanel are only available for cars. If you have other members on the ControlPanel that need to be open, or the outside world needs to keep a link to the ControlPanel at some point, consider using the interface to expose only those members that you want to publish.

 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 

Not necessary...

 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 you are trying to achieve in terms of API?

+4
source share

First, let's clarify the terminology. What you're talking about is a "nested" class, not a subclass

I do not believe that what you are trying to do is possible in .NET. However, do you really need this? If cp is private in the car, no one can increase the odometer on this car control panel. They could create their own control panel, which is not part of the car, and then mess around with it, but what harm is it?

0
source share

Make your ControlPanel class private and its properties public. The ControlPanel class will only be displayed for the Car type, but Car will still be able to change the ControlPanel properties.

0
source share

Nested classes are one way to go, or you can have your Car as an integral class. The control panel can be used in many different cars (subclasses), so why insert them into a slot? The car has a control panel. The car has an engine

Bus: a car also has such things.

A radio may be something that has a control panel. The speedometer may be part of the control panel, which may also be part of the control panel.

Then you get

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

How about a motorcycle? This is not a car, but it still has a dashboard with a speedometer.

Get my thought? Composite objects.

0
source share

Source: https://habr.com/ru/post/651273/


All Articles