failure to parse m or m/s

Feb 20, 2014 at 11:50 PM
I'm trying to make a method that takes two unit strings and performs a conversion. Can you tell me why the code below doesn't work? I cannot seem to find anything that will successfully parse.
private readonly UnitProvider _up = new UnitProvider(typeof(UnitProvider).Assembly, CultureInfo.InvariantCulture);
public double Convert(double value, string currentUnit, string targetUnit)
{
    IQuantity currentQuantity, targetQuantity;

    if(!_up.TryParse(currentUnit, CultureInfo.InvariantCulture, out currentQuantity))
        throw new ArgumentException("Cannot parse " + currentUnit, "currentUnit");

    if(!_up.TryParse(targetUnit, CultureInfo.InvariantCulture, out targetQuantity))
        throw new ArgumentException("Cannot parse " + targetUnit, "targetUnit");

    return currentQuantity.MultiplyBy(value).ConvertTo(targetQuantity);
}
Coordinator
Feb 23, 2014 at 8:19 PM
Try
        public double Convert(double value, string currentUnit, string targetUnit)
        {
            var currentQuantity = _up.GetUnit(currentUnit);
            var targetQuantity = _up.GetUnit(targetUnit);
            return currentQuantity.MultiplyBy(value).ConvertTo(targetQuantity);
        }
When using the TryParse method, the unit type must match the output quantity type.
Feb 24, 2014 at 3:51 PM
Your proposed solution works when converting cm to m. However, converting mi to m gives me this:

InvalidCastException: Specified cast is not valid.
at \Source\Units\Quantities\Length.cs:line 598

and converting m to mi gives me this:

InvalidCastException: Specified cast is not valid.
at \Source\Units\Quantities\TypographicLength.cs:line 486

I think I should be able to convert between meters and miles without a problem.
Coordinator
Feb 24, 2014 at 4:32 PM
Then you should let the library know that you work with units of length:
        public double Convert(double value, string currentUnit, string targetUnit)
        {
            IQuantity currentQuantity;
            _up.TryGetUnit(typeof(Length), currentUnit, out currentQuantity);
            IQuantity targetQuantity;
            _up.TryGetUnit(typeof(Length), targetUnit, out targetQuantity);
            return currentQuantity.MultiplyBy(value).ConvertTo(targetQuantity);
        }
Alternative: unregister the conflicting units.
Feb 24, 2014 at 5:22 PM
What are the conflicting units in my example? "m" or "mi" refer to something other than meters and miles in some situation? I don't really know that I'm doing a length conversion. It could be one of several different types of conversion. I do know that currentUnit and targetUnit will refer to the same general type of quantity.
Coordinator
Feb 25, 2014 at 12:16 PM
Yes, currently "m" is not unique in the default unit provider. It is also registered for the TypograficLength quantity type.
You can create a new unit provider, and register the quantity types (which implicit registers the units of each type) you want to support.
You can also explicitly register the units you want to convert. See recently added unit tests in UnitProviderTests.