I'm implementing value types which represents a distance (or length). There's an enum that represents the different units of measure, eg:
public enum DistanceUnit
{
Millimeter,
Centimeter,
Meter,
Kilometer,
Inch,
Foot,
Yard,
Mile
};
These measurements fall into one of two systems - either metric or imperial. Since enum doesn't support hierachies, what's the best pattern for representing this hierarchy?
Is it to use bit flags? Or use two seperate enums along with some methods to correlate them? Or to declare static members instead of enums?
Give me some advice... how would you implement this?
Edit - More clarification: I have several immutable structs (generated by T4) which represent various measurements:
public struct Meters : IEquatable<Distance>,
IEquatable<Meters>,
IEquatable<Millimeters>, ... IComparable<> etc.. etc..
{
public readonly decimal Value;
...
public static implicit operator Distance (Meters other) {
// Can implicitly cast to Distance
}
}
public struct Millimeters ...
public struct Centimeters ....
... etc, as well as a hand-coded immutable Distance, intended to represent any measure:
public struct Distance : IEquatable<Distance>,
IEquatable<Meters>,
IEquatable<Millimeters>, ...
IFormattable
{
public readonly decimal Value;
public readonly DistanceUnits UnitOfMeasure;
...
public string ToString(string format, IFormatProvider provider)
{
// Logic:
// If UOM = Meters and Value < .1 then display as "10 cm" instead of ".1 M"
// If UOM = Inches and value multiple of 12, display as feet
// etc, etc
}
}
Foregoing a discussion about whether the Distance should be converted to the correct UOM by calling code, the goal here is for ToString to convert the value up or down on the same measurement (imperial or metric) that is represented by the current UnitOfMeasure.
Obviously this could all be hard coded into the ToString method, but given that I'm also implementing TypeConverters and FormatProviders for this whole shibang, I'd like to find a generic means of figuring out, from a DistanceUnit, what the appropriate next-up or next-down unit of measure would be.
Am I barking up the wrong tree by wanting to implement in this manner?