How to convert a measurement to a different unit


Summary This topic describes how to convert a measurement in one unit to another, for example convert a 10 kilometres to miles, or a latitude of 51.54 in decimal degrees to degrees, minutes and seconds. It also describes some limitations of conversions.

To use the code in this topic, add references to ESRI.ArcGISExplorer.dll and ESRI.ArcGISExplorer.Application.dll. Also add the following namespace references via using (C#) or Imports (VB .NET) statements:
  • ESRI.ArcGISExplorer.Geometry
You can use the Unit class to help convert measurements from one unit to another. For example, use the static (Shared in Visual Basic)  Convert method as shown below to change a linear measurement from miles to kilometers, an angular measurement from arc-minutes to decimal minutes, or an area measurement from hectates to acres.
[C#]
// Convert linear distance from metric to imperial - kilometers to miles.
double miles = Unit.Convert(10, Unit.Linear.Kilometers, Unit.Linear.MilesStatute);
// Convert arc-minutes into decimal minutes.
double decimalMins = Unit.Convert(90, Unit.Angular.Minutes,
    Unit.Angular.MinutesCentesimal);
// Convert area from metric to imperial - hectares to acres.
double hectares = Unit.Convert(1, Unit.Area.Hectares, Unit.Area.Acres);
[VB.NET]
' Convert linear distance from metric to imperial - kilometers to miles.
Dim miles As Double = Unit.Convert(10, Unit.Linear.Kilometers, Unit.Linear.MilesStatute)
' Convert arc-minutes into decimal minutes.
Dim decimalMins As Double = Unit.Convert(90, Unit.Angular.Minutes, Unit.Angular.MinutesCentesimal)
' Convert area from metric to imperial - hectares to acres.
Dim hectares As Double = Unit.Convert(1, Unit.Area.Hectares, Unit.Area.Acres)
Measurements can only be converted directly between units of the same UnitType, for example linear to linear, angular to angular, or area to area. If you attempt to convert from linear to area or angular units for example*, an ExplorerException will be thrown.
Angular units are not directly convertible to linear units, as angular units change in terms of the linear distance they represent depending on where they are located on the earth. If you have a geographical location for the measurement, you may wish to refer to the GeodesicUtilities class.
Overloaded Convert and TryConvert instance methods are available which convert from the units of a specific Unit object. 
If you are unsure if a conversion will succeed, you may wish to use the TryConvert method instead as shown below. This method will not throw an exception but instead return a boolean value indicating whether the conversion is successful or not (the actual converted measurement is also available as an out (ByRef in Visual Basic) parameter).
[C#]
// Get a reference to the MapDisplay.
ESRI.ArcGISExplorer.Mapping.MapDisplay disp =
    ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay;
// Try converting a measurement to the units of the MapDisplay.
double measurement;
bool converted = disp.CurrentCoordinateSystem.Unit.TryConvert(10, Unit.Linear.Feet,
    out measurement);
[VB.NET]
' Get a reference to the MapDisplay.
Dim disp As ESRI.ArcGISExplorer.Mapping.MapDisplay = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay
' Try converting a measurement to the units of the MapDisplay.
Dim measurement As Double
Dim converted As Boolean = disp.CurrentCoordinateSystem.Unit.TryConvert(10, Unit.Linear.Feet, measurement)
TryConvert is commonly used if, instead of hard-coding a specific Unit, a Unit object reference is passed in to the conversion method, as you may not be able to check at compile time that the two units are the same type - in the example above, the MapDisplay units are used.


See Also:

Unit.Convert method
Unit.TryConvert method
GeodesicUtilities class