Click here to Skip to main content
15,891,316 members
Articles / Programming Languages / C#

Working with Units and Amounts

Rate me:
Please Sign up or sign in to vote.
4.99/5 (76 votes)
17 Jul 2013CPOL17 min read 87.6K   1.5K   150  
The ultimate Unit and Amount classes for your business and physics applications!
using System;
using System.Runtime.Serialization;

namespace TypedUnits
{
	/// <summary>
	/// Exception thrown when a unit conversion failed, i.e. because you are converting
	/// amounts from one unit into another non-compatible unit.
	/// </summary>
	[Serializable]
	public class UnitConversionException : InvalidOperationException
	{
		public UnitConversionException() : base() { }

		public UnitConversionException(string message) : base(message) { }

		public UnitConversionException(Unit fromUnit, Unit toUnit) : this(String.Format("Failed to convert from unit '{0}' to unit '{1}'. Units are not compatible and no conversions are defined.", fromUnit.Name, toUnit.Name)) { }

		protected UnitConversionException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{ }
	}

	/// <summary>
	/// Exception thrown whenever an exception is referenced by name, but no
	/// unit with the given name is known (registered to the UnitManager).
	/// </summary>
	[Serializable]
	public class UnknownUnitException : ApplicationException
	{

		public UnknownUnitException() : base() { }

		public UnknownUnitException(string message) : base(message) { }

		protected UnknownUnitException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{ }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect AREBIS
Belgium Belgium
Senior Software Architect and independent consultant.

Comments and Discussions