Skip to content

neoeinstein/csunits

 
 

Repository files navigation

csunits

Simple C# framework to support Units of Measurement

Copyright 2011 © Anders Gustafsson, Cureos AB.

Made available under Eclipse Public License, version 1.0.

Prerequisites

Visual Studio 2010 Service Pack 1 or higher with Portable Library Tools (msdn.microsoft.com/en-us/library/gg597391.aspx) extension.

Introduction

Developed with Visual Studio 2010, using .NET Frameworks 3.5 and 4.0. The .NET 4.0 class library is portable and can without modifications be included in Silverlight 4 and Windows Phone (“Mango”) applications. Source code can also be compiled using Mono, at least versions 2.8 and later.

Measure<Q> and StandardMeasure<Q> are the main “work-horses” of the library. StandardMeasure<Q> is always represented in the reference unit of the associated quantity. If a different unit is specified in instantiation of StandardMeasure<Q>, the measured amount is automatically converted to the equivalent reference unit amount. On the other hand, the amount and unit used in instantiation of Measure<Q> are internally maintained.

StandardMeasure<Q> is declared as a struct and only holds one member, the amount. The main goal of this approach is to maximize calculation performance, while at the same time ensuring quantity type safety.

There are also StandardMeasureDoublet<Q1, Q2> and StandardMeasureTriplet<Q1, Q2, Q3> structures that holds two and three measures, respectively, of potentially different quantities.

Usage examples

using Cureos.Measures;
using Cureos.Measures.Quantities;
...

StandardMeasure<Mass> initialWgt = new StandardMeasure<Mass>(75.0);
StandardMeasure<Mass> gainedWgt = new StandardMeasure<Mass>(2.5, Mass.HectoGram);
StandardMeasure<Mass> newWgt = initialWgt + gainedWgt;

Measure<Mass> newWgtInGram = newWgt[Mass.Gram];
Measure<Mass> initialWgtInGram = newWgtInGram - gainedWgt;

Console.WriteLine("Initial weight: {0}", initialWgtInGram);

Measure<Length> height = new Measure<Length>(30.0, Length.CentiMeter);
StandardMeasure<Area> area = (StandardMeasure<Area>)0.02;

StandardMeasure<Volume> vol; 
ArithmeticOperations.Times(height, area, out vol);
var maxVol = new StandardMeasure<Volume>(10.0, Volume.Liter);

if (vol < maxVol)
{
  Console.WriteLine("Calculated volume is within limits, actual volume: {0}", vol[Volume.Liter]);
}

should yield the output:

Initial weight: 75000 g
Calculated volume is within limits, actual volume 6 l

Application

For pure demonstration and testing purposes, there are very simple Silverlight 4 and Windows Phone (“Mango”) unit converter applications included in the solution, SilverlightUnitConverter and WP7UnitConverter, respectively. Implementation of these applications were inspired by Andrew Cheng’s feedback and usage of csunits in his Windows Forms application.

Revision

Last updated on August 3, 2011 by Anders Gustafsson, anders[at]cureoscom, www.cureos.com.

About

C# framework for Units of Measurement

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published