C# 从嵌套类访问高级方法

发布于 2024-12-05 21:33:54 字数 1817 浏览 1 评论 0原文

我想为通过串行端口进行通信的外部控制电路创建一个库类。该电路具有内置功能,可使用串行通信获取/设置各种设置(例如发送“SR,HC,01,1,\r”打开传感器1)。有大约 100 个功能分为以下类别:传感器设置、输出设置和环境设置。这是我尝试过的。

public class CircuitController
{
   // Fields.
   private SerialPort controllerSerialPort;
   private SensorSettings sensorSettings;
   private OutputSettings outputSettings;
   private EnvironmentSettings environmentSettings;
   ...

  // Properties.
  // Properties to get sensorSettings, outputSettings, and environmentSettings.

  // Methods.
  public string SendReceive(string sendCommand)   // Send command and receive response.
  {
     ...
  }

  // Nested classes.
  public class SensorSettings
  {
     // Fields.
     // The various sensor settings here.

     // Properties.
     // Properties to get/set the sensor settings. Note: Get/Set is done through calling one of the following methods.

     // Methods.
     public double GetSensorUnits(int sensorNumber)
     {
        ...
        string commandToSend = String.Format("HE,WL,1,{0}", sensorNumber);   // Setup command string.
        string commandResponse = SendReceive(commandToSend);   // Send command and receive response. ERROR here, cannot access higher level, non-static methods.
        // Logic to process commandResponse.
        ...
     }

     // Other methods to create, send, and process the circuit's sensor settings "functions".

  }

  public class OutputSettings
  {
     // Same logic as SensorSettings class.
  }

  public class EnvironmentSettings
  {
     // Same logic as SensorSettings class.
  }
}

我认为这样在 CircuitController 类下就不会塞满 100 个方法/属性。例如,我可以使用 get 属性来获取sensorSettings 实例,然后调用所需的方法/属性:CircuitControllerInstance.GetSensorSettingsProperty.GetSensorUnits(1);。我尝试从嵌套类访问 SendReceive() 时收到编译错误。有办法做到这一点吗?

谢谢!

I want to create a library class for an external control circuit which communicates via serial port. The circuit has built in functions to get/set various settings using serial communication (e.g. sending "SR,HC,01,1,\r" turns sensor 1 ON). There are ~100 functions sorted into the following categories: Sensor Settings, Output Settings, and Environment Settings. Here is what I tried.

public class CircuitController
{
   // Fields.
   private SerialPort controllerSerialPort;
   private SensorSettings sensorSettings;
   private OutputSettings outputSettings;
   private EnvironmentSettings environmentSettings;
   ...

  // Properties.
  // Properties to get sensorSettings, outputSettings, and environmentSettings.

  // Methods.
  public string SendReceive(string sendCommand)   // Send command and receive response.
  {
     ...
  }

  // Nested classes.
  public class SensorSettings
  {
     // Fields.
     // The various sensor settings here.

     // Properties.
     // Properties to get/set the sensor settings. Note: Get/Set is done through calling one of the following methods.

     // Methods.
     public double GetSensorUnits(int sensorNumber)
     {
        ...
        string commandToSend = String.Format("HE,WL,1,{0}", sensorNumber);   // Setup command string.
        string commandResponse = SendReceive(commandToSend);   // Send command and receive response. ERROR here, cannot access higher level, non-static methods.
        // Logic to process commandResponse.
        ...
     }

     // Other methods to create, send, and process the circuit's sensor settings "functions".

  }

  public class OutputSettings
  {
     // Same logic as SensorSettings class.
  }

  public class EnvironmentSettings
  {
     // Same logic as SensorSettings class.
  }
}

I figured that this way there won't be 100 methods/properties crammed under the CircuitController class. I could use a get property to obtain the sensorSettings instance, for example, and then call the desired method/property: circuitControllerInstance.GetSensorSettingsProperty.GetSensorUnits(1);. I receive a compile error that I am trying to access SendReceive() from a nested class. Is there a way to do this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不美如何 2024-12-12 21:33:54

嵌套类不会“看到”其宿主中声明的任何内容。

您应该将主机引用传递给任何嵌套类,例如在构造函数中。

A nested class does not "see" whatever is declared in its host.

You should pass a host reference to any nested class, for instance, in the constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文