I am thinking what I want to do is impossible but thought I would ask anyway. I am thinking of implementing some kind of custom conversion between different metric measurements - such as converting inches to metres and other units.
I am thinking base class called Unit as follows. NOTE: I have not put in any fields to hold the number of units eg 2 metres, 5 inches and so on:
public abstract class Unit {
protected string _name;
public Unit(string name)
{
_name = name;
}
}
Then subclasses of Unit for Metre and Inch:
public class Metre : Unit {
public Metre() : base("Metre")
{
}
}
public class Inch : Unit {
public Metre() : base("Inch")
{
}
}
I would like to have a class that could handle conversion of these units between one another. Something like:
public static class UnitConvertor
{
public Unit Convert(Unit from, Type to) : where Type extends/inherits from Unit
{
// do the conversion
return the instance of Type to;
}
}
Any thoughts?