我創建了一個庫類CircuitController
與通過串口控制電路通信。C#方法與屬性:異常可能會出現
public class CircuitController
{
// Enumerations.
public enum Sensors { Sensor1, Sensor2, ..., Sensor15 };
...
// Fields.
private SerialPort serialPort; // Set in constructor.
private Dictionary<Sensors, Sensor> activeSensorCollection;
...
// Properties.
public Dictionary<Sensors, Sensor> ActiveSensors
{ get { return this.activeSensorCollection; } }
// Methods.
public void SetSensorUnits(Sensors sensor, String sensorUnits)
{
// Creates serial command based off parameters, sends, receives, processes.
}
...
// Constructors.
public CircuitController(...)
{
... // Set CircuitController fields including nested classes.
Sensor sensor1 = new Sensor(a,b,c,d,this); // Link sensor to this controller instance.
... // Add sensors to dictionary.
}
// Nested Classes.
public class Sensor
{
// Fields.
private CircuitController controller;
private String units;
private Sensors sensorNumber;
...
// Properties.
public String Units
{
get
{
return this.controller.GetSensorUnits(this.sensorNumber);
}
set
{
this.controller.SetSensorUnits(this.sensorNumber, value);
}
}
...
}
因此,這裏是我的問題:是否可以將允許用戶獲取/通過屬性設置傳感器的設置,即使一個可能拋出異常(例如串行通信錯誤)?
String sensor2Units = circuitControllerInstance.ActiveSensors[Sensor2].Units'
circuitControllerInstance.ActiveSensors[Sensor1].Units = "mm";
我覺得這是更清楚比...
String sensor2Units = circuitControllerInstance.GetSensorUnits(Sensors.Sensor2);
circuitControllerInstance.SetSensorUnits(Sensors.Sensor1, "mm");
請注意,這些都是簡化的例子,一些get/set方法有多達5個參數這將是一個麻煩反覆輸出。 :/
我同意。這是要走的路。 –
太好了。謝謝。 – john