Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Inheritance: Static vs. Non-Static Field
    primarykey
    data
    text
    <p>I have a library base class (<code>Controller</code>) and three sub-classes that inherit from <code>Controller</code> (<code>Sensor</code>, <code>Output</code>, and <code>Environment</code>) where:</p> <pre><code>public class Controller { private SerialPort serial; private Sensor sensorSettings; private Output outputSettings; private Environment environmentSettings; protected Dictionary&lt;int, string&gt; ErrorDescriptions { get{ this.errorDescriptions; } } protected SerialPort ControllerSerialPort { get{ this.serial; } } protected Sensor SensorSettings { get{ this.sensorSettings; } } // The other sub-class get properties. protected string SendReceive(string controllerCommand) { ... } ... } public class Sensor : Controller {...} ... // Other sub-classes </code></pre> <p>My question is this: Should I make the <code>errorDescriptions</code> static? These error descriptions will not change from controller to controller (i.e. static) but I wasn't sure what happens in the inherited classes. Will I have to refer to them as <code>Sensor.errorDescription</code> in the <code>Sensor</code> class or will it still be <code>Controller.errorDescription</code>?</p> <p><strong>EDIT</strong></p> <p>Wow, I just realized I messed up big time. Here's how it should be, I think:</p> <pre><code>private abstract class ControllerBasics { protected SerialPort serial; // The serial port to communicate with the controller. protected Dictionary&lt;int, string&gt; errorDescriptions = new Dictionary&lt;int, string&gt; {...}; // Possible errors for the controller (known and fixed). Won't change from controller to controller. public enum Sensors {One, Two, ...}; // Possible sensor selection. public string SendReceiveCommand(string command){...} // Method to send string command over "serial". } public class OverallController : ControllerBasics // The controller class. { // Add top-level controller settings. private string controllerName = "Controller1"; // Have a property to get/set. private bool controllerON; // Controller on/off. Have property to get/set. ... // Other similar fields and methods. // Used to "sort" the controller's many settings/functions. private SensorSettings sensorSettings; // Have get/set properties for these so I could do the following: overallControllerInstance.GetSensorSettingsProperty.SetActiveSensorCount(5); private OutputSettings outputSettings; private EnvironmentSettings environmentSettings; public OverallController(string name, string comPort, ...) // Constructor. { // Basic settings initialization. // Create serial port. this.sensorSettings = new SensorSettings(this.serial); this.outputSettings = ... } public class SensorSettings : ControllerBasics // Class to hold the controller's specific sensor settings and their respective get/set methods. Not a new type of controller. { private int activeSensorCount; // Have public method to get/set. ... // Others. public void SetActiveSensorCount(int sensorCount) { // Send command using inherited SendReceive(). } ... // Others. } public class OutputSettings : ControllerBasics // Same logic as SensorSettings. { private string units; // Have public method to get/set. ... // Others. public string GetUnitType() // Meters, mm, um... { // Send command using inherited SendReceive(). } ... // Others. } public class EnvironmentSettings : ControllerBasics // Same logic as SensorSettings. { ... } </code></pre> <p>So if <code>errorDescriptions</code> defined in <code>ControllerBasics</code> is known and fixed (and required in all derived classes) should I make it static or should I just leave it protected and each derived class will have it's own dictionary (i.e. this.errorDescriptions)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload