Click here to Skip to main content
Licence CPOL
First Posted 26 Dec 2007
Views 14,768
Downloads 333
Bookmarked 10 times

Complete Unit Conversion Sample in C# .NET

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

About the Author

Antonom

Software Developer

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 26 Dec 2007
Article Copyright 2007 by Antonom
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid