This measurement conversion applet demonstrates some of the core features of Inner Drive Extensible Architecture™—the Idea™.
* = default unit for the measurement class.
You're using the English (United States) culture.
Internationalization and localization are built into the framework from the start. But we're still missing a few translations here and there (which is why you sometimes see a mix of U.S. English and other languages). So please help us by help us by reporting translation errors.
We're pleased to announce that we've put a partial SDK up on the site, so you can see what we've been up to. (Important note: This is pre-release documentation, subject to change at any point.)
Each of the principal types of measurement—area, length, speed, etc.—is a distinct .NET structure. This means that the compiler, not the developer, controls what kind of measurements are possible for a particular phenomenon.
However, all of the built-in measurement structures implement IMeasurable, which defines a number of properties and methods useful for a generic converter like this one.
The framework also defines an abstract Unit class, and a collection of concrete measurement units that derive from it. Each contains its own list of conversion factors, meaning that measurements can convert directly to other measurements without going through an intermediary; and the conversion framework is completely extensible, since new Unit classes can simply define their own conversion list.
Converting measurements becomes trivial in most cases:
Length meters = new Length(3000d); // defaults to meters
Length feet = meters.ConvertTo(typeof(Foot));
// feet.ToString("0.0000") yields "9842.5197 ft."
Even compound units, such as those used for Speed and Pressure, are easy to handle:
ForceOverArea psi = new ForceOverArea(new Pound(), new InchSquare()); Pressure pressure = new Pressure(29.92d, psi); Pressure atmospheres = pressure.ConvertTo(typeof(Atmosphere));
(This, incidentally, is why Speed and Pressure have so few conversions available in this demo. We are working on making the demo aware of compound units.)
But even in a generic case, like this conversion utility, the appropriate use of interfaces and abstractions makes it not much more complicated than the more common, concrete case. This is the salient code behind this Web form:
using InnerDrive.Quantitative;
private void Convert()
{
Unit fromUnit = CreateUnit(convertFromList.SelectedValue);
Type toUnit = GetType(convertToList.SelectedValue);
double rawValue = 0;
double.TryParse(convertFromBox.Text, out rawValue);
string result = string.Empty;
try
{
IMeasurable from = CreateMeasure(measureTypeList.SelectedValue, rawValue, fromUnit);
IMeasurable converted = from.ConvertTo(toUnit);
string fromText = FormattedMeasure(from);
string toText = FormattedMeasure(converted);
result = String.Format("{0} = {1}", fromText, toText);
}
catch
{
// Handle various exceptions
}
resultText.Text = result;
}
private IMeasurable CreateMeasure(
string className,
double value,
Unit unit)
{
Assembly assembly = typeof(Time).Assembly; // Cheating, and limiting to IDT classes
object[] args = GetArguments(value, unit);
Type type = assembly.GetType(className);
return (IMeasurable)System.Activator.CreateInstance(type, args);
}
private string FormattedMeasure(IMeasurable measure)
{
string formatText =
measure.ToString(DefaultFormat, MetricExponent.Unit, CultureInfo.CurrentUICulture);
if (measure.Value == 0d)
return formatText;
// Multiple 'if' block is quick and dirty; a more elaborate, reflection-based
// approach is possible
if (measure.Value < .0001d)
{
formatText =
measure.ToString(DefaultFormat, MetricExponent.Micro, CultureInfo.CurrentUICulture);
}
else if (measure.Value < .1d)
{
formatText =
measure.ToString(DefaultFormat, MetricExponent.Milli, CultureInfo.CurrentUICulture);
}
else if (measure.Value > 100d)
{
formatText =
measure.ToString(DefaultFormat, MetricExponent.Kilo, CultureInfo.CurrentUICulture);
}
else if (measure.Value > 100000d)
{
formatText =
measure.ToString(DefaultFormat, MetricExponent.Mega, CultureInfo.CurrentUICulture);
}
return formatText;
}
private Unit CreateUnit(string className)
{
object unit = System.Activator.CreateInstance(GetType(className));
return (InnerDrive.Quantitative.Unit)unit;
}
private Type GetType(string className)
{
Assembly assembly = typeof(Time).Assembly;
return assembly.GetType(className, false);
}
InnerDrive.Quantitative 1.10.3121.0 Release CLR 2.0.50727.1433
Copyright ©2008 Inner Drive Technology. All rights reserved. Portions Copyright ©1986-2008 David Braverman.
Page info
Page last modified 2006-04-09. Legal information is current as of 2006-04-09.
Copyright ©2008 Inner Drive Technology. Purveyors of fine, hand-crafted computer software.
Copyright ©2008 Inner Drive Technology. Purveyors of fine, hand-crafted computer software. Legal information. Page printed 2008-09-05T21:27:54 UTC.