Click here to Skip to main content
15,886,046 members
Articles / Web Development / ASP.NET
Article

Complete Unit Conversion Sample in C# .NET

Rate me:
Please Sign up or sign in to vote.
3.27/5 (8 votes)
26 Dec 2007CPOL 46.7K   1.2K   13  
How To Convert { Cm Px In Mm } Unit ?
Screenshot -

Introduction

This sample has no introduction...

Using the Code

Make UnitTypes enumeration like this:

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

Make a Unit DataType structure like this:

C#
[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:

C#
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --