I’ve been working on some electrical network simulation software (ElecNetKit). In electrical networks, sometimes it’s convenient to work with single-phase models, sometimes in three-phase models.
As such, I would like to be able to represent one of the electrical network elements as:
class Bus
{
public Complex Voltage {set; get;} //single phase property
}
but simultaneously in a fashion so that the user can call Bus.Voltage.Phases[x], and expect a Complex for any valid integer x.
The Bus.Voltage property should map to Bus.Voltage.Phases[1] when treated as a Complex.
I’ve got two questions here:
- Is this in violation of any OOP principles? I’ve got a feeling that it might be.
- Is there a convenient way to represent this in C#?
In terms of representation, I’ve tried:
- a class
Phased<T> : T, but this is incompatible with the typing system, and - a class
Phased<T>with a generic converter to typeT, but the converter still needs to be invoked.
I’m aware that I can simply use something like:
public Dictionary<int,Complex> VoltagePhases {private set; get;}
public Complex Voltage {
set {VoltagePhases[1] = value;}
get {return VoltagePhases[1];}
}
but there’s a lot of repetition once you start to do this for multiple properties, across multiple classes.
I would propose something like this: