Geeks With Blogs
My Blog

Goals

  • Create a class that associates a numeric quantity with a unit of measurement.
  • Provide support for simple arithmetic and comparison operations.

Implementation

  • An immutable class (Could have been struct but I may try inheritance later)
  • Unit is stored in an enumeration
  • Supported operations:
  • Addition w/ like units
  • Subtraction w/ like units
  • Multiplication by scalar
  • Division by scalar
  • Modulus by scalar
  • Equals()
  • >, >=, <, <=, ==
  • IComparable
  • ToString()
  • Implicit cast to Decimal

The Source

The souce can be downloaded from Github.

Notes

  • This class does not support any arithmetic that would modify the unit.
  • This class is not suitable for manipulating currencies.

Future Ideas

  • Have a CompositeQuantity class that would allow quantities with unlike units to be combined.
  • Similar currency class with support for allocations/distributions.
  • Provide conversion between units. (Actually I think this would be best placed in an external service. Many situations I deal with require some sort of dynamic conversion ratio.)
Posted on Monday, May 17, 2010 8:10 PM | Back to top


Comments on this post: A Quantity class with units

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Ryan Ohs | Powered by: GeeksWithBlogs.net