Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Complete Unit Conversion Sample in C# .NET

0.00/5 (No votes)
26 Dec 2007 2  
How To Convert { Cm Px In Mm } Unit ?
Screenshot -

Introduction

This sample has no introduction...

Using the Code

Make UnitTypes enumeration like this:

public enum UnitTypes
{
    Cm,
    In,
    Mm,
    Px,
}

Make a Unit DataType structure like this:

[TypeConverter(typeof(UnitConverter))]

public struct Unit
{
public Unit(double value, UnitTypes type)
{
this._value = value;
this._type = type;
}
private double _value;
public double Value
{
get { return _value; }
set { _value = value; }
}
private UnitTypes _type;
public UnitTypes Type
{
get { return _type; }
set
{
this.Value = this.To(value).Value;
_type = value; 
}
}
public double GetPixelPer(UnitTypes unitType)
{
switch (unitType)
{
case UnitTypes.Cm:
return this.GetPixelPer(UnitTypes.In) / 2.54F;
case UnitTypes.In:
return 96;
case UnitTypes.Mm:
return this.GetPixelPer(UnitTypes.Cm) / 10;
default:
return 1;
}
}
public Unit To(UnitTypes unitType)
{
return new Unit((this.Value * this.GetPixelPer(this.Type)) / 
				this.GetPixelPer(unitType), unitType);
}
public static Unit operator +(Unit a, Unit b)
{
return new Unit(a.To(UnitTypes.Px) + b.To(UnitTypes.Px), UnitTypes.Px);
}
public static Unit operator -(Unit a, Unit b)
{
return new Unit(a.To(UnitTypes.Px) - b.To(UnitTypes.Px), UnitTypes.Px);
}
public static Unit operator *(Unit a, Unit b)
{
return new Unit(a.To(UnitTypes.Px) * b.To(UnitTypes.Px), UnitTypes.Px);
}
public static Unit operator /(Unit a, Unit b)
{
return new Unit(a.To(UnitTypes.Px) / b.To(UnitTypes.Px), UnitTypes.Px);
}
public static implicit operator double(Unit u)
{
return u.To(UnitTypes.Px);
}
public override string ToString()
{
return string.Format("{0} {1}", this.Value.ToString(), this.Type.ToString());
}
}

Make a class that inherited from TypeConverter in System.ComponentModel namespace like this:

public class UnitConverter
: TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo
	(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom
	(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string unitString = (value as string);
double unitValue = -1;
if (double.TryParse(unitString.Remove(unitString.Length - 2, 2), out unitValue))
return new Unit(unitValue, (UnitTypes)Enum.Parse(typeof(UnitTypes), 
		unitString.Substring(unitString.Length - 2, 2), true));
else throw new Exception("Property value is not correct. 
	( { ±5.0 × 10-324 to ±1.7 × 10308 } { Cm | Px | In | Mm } )");
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, 
	CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return value.ToString();
else return base.ConvertTo(context, culture, value, destinationType);
}
}

Then test your Unit DataType with Console or other Application types... like this:

using System;
using System.ComponentModel;
using System.Globalization;
namespace UnitConverterSample
{
public static class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Welcome to UnitConverter Test Center...");
Unit a = new Unit(1, UnitTypes.In);
Console.WriteLine("{0} is {1}", a.ToString(), a.To(UnitTypes.Cm).ToString());
Console.WriteLine("{0} is {1}", a.ToString(), a.To(UnitTypes.Mm).ToString());
Console.WriteLine("{0} is {1}", a.ToString(), a.To(UnitTypes.In).ToString());
Console.WriteLine("{0} is {1}", a.ToString(), a.To(UnitTypes.Px).ToString());
Unit b = new Unit(1, UnitTypes.Cm);
Console.WriteLine("{0} is {1}", b.ToString(), b.To(UnitTypes.Mm).ToString());
Console.WriteLine("{0} is {1}", b.ToString(), b.To(UnitTypes.In).ToString());
Console.WriteLine("{0} is {1}", b.ToString(), b.To(UnitTypes.Cm).ToString());
Console.WriteLine("{0} is {1}", b.ToString(), b.To(UnitTypes.Px).ToString());
Console.WriteLine("Start Conversion Test...");
Unit e = new Unit(10, UnitTypes.Cm);
Console.WriteLine("Before Unit Conversion : {0} .", e.ToString());
e.Type = UnitTypes.Px;
Console.WriteLine("After Unit Conversion : {0} .", e.ToString());
e.Type = UnitTypes.Cm;
Console.WriteLine("After Rollback Unit Conversion : {0} .", e.ToString());
Console.ReadLine();
}
}
}

Thank you.

MGA2005
Mohammad.MGA@hotmail.com

History

  • 26th December, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here