MS.COM Operations Tools Team WebLog

Hey - What does this button do?

Unheard of temperatures

I was recently working on a thermometer control and decided to write a temperature class to go along with it.  What I found interesting is that there are temperature systems that I had never even heard of let alone used (namely Reaumer and Rankine).  I'm from Canada and I've heard of some pretty nasty temperatures, but these were still a suprise to me. :-)  Anyway I wrote the class in C# and thought others might find some use in it.   Here it is:

 

using System;

namespace ThermometerDemo
{
    /// <summary>
    /// Summary description for Temperature.
    /// </summary>
    public class Temperature
    {
        public enum SystemOfMeasure { Fahrenheit = 0, Celsius, Kelvin, RanKine, Reaumer };

        private SystemOfMeasure _systemOfMeasure;
        public SystemOfMeasure Scale
        {
            get { return _systemOfMeasure; }
            set { _systemOfMeasure = value; }
        }

        private double _temperatureKelvin;
        public double Value
        {
            get { return Temperature.Convert( _temperatureKelvin, SystemOfMeasure.Kelvin, _systemOfMeasure ); }
            set { this._temperatureKelvin = Temperature.Convert( value, _systemOfMeasure , SystemOfMeasure.Kelvin ); }
        }

        public double AbsoluteZero
        {
            get { return Temperature.Convert( 0F, SystemOfMeasure.Kelvin, _systemOfMeasure ); }
        }


        #region Construction

        public Temperature() : this( SystemOfMeasure.Kelvin, 298.15F )
        {
            // SI temperature at standard state in Kelvin.
        }

        public Temperature( SystemOfMeasure systemOfMeasure ) : this( systemOfMeasure, 298.15F )
        {
            // SI temperature at standard state.
        }

        public Temperature( double temperature ) : this( SystemOfMeasure.Kelvin, temperature )
        {
        }

        public Temperature( SystemOfMeasure systemOfMeasure, double temperature )
        {
            _systemOfMeasure = systemOfMeasure;
            Value = temperature;
        }

        #endregion Construction


        # region Public Methods

        public static double Convert( double temperature, SystemOfMeasure originSystem, SystemOfMeasure targetSystem )
        {
            // Convert to Kelvin.
            double temperatureKelvin;
            switch( originSystem )
            {
                case SystemOfMeasure.Fahrenheit:
                    temperatureKelvin = ( 5F / 9F ) * ( temperature - 32 ) + 273.15F;
                    break;

                case SystemOfMeasure.Celsius:
                    temperatureKelvin = temperature + 273.15F;
                    break;

                case SystemOfMeasure.Kelvin:
                    temperatureKelvin = temperature;
                    break;

                case SystemOfMeasure.Reaumer:
                    temperatureKelvin = ( 5F / 9F ) * temperature + 273.15F;
                    break;

                case SystemOfMeasure.RanKine:
                    temperatureKelvin = temperature / 1.8F;
                    break;

                default:
                    throw new ArgumentOutOfRangeException( "originSystem", originSystem, "Unsupported origin system of measure");
            }

            // Convert to the new system.
            double resultTemperature;
            switch( targetSystem )
            {
                case SystemOfMeasure.Fahrenheit:
                    resultTemperature = 32F + ( 1.8F ) * ( temperatureKelvin - 273.15F );
                    break;

                case SystemOfMeasure.Celsius:
                    resultTemperature = temperatureKelvin - 273.15F;
                    break;

                case SystemOfMeasure.Kelvin:
                    resultTemperature = temperatureKelvin;
                    break;

                case SystemOfMeasure.Reaumer:
                    resultTemperature = ( 1.8F ) * ( temperatureKelvin - 273.15F );
                    break;

                case SystemOfMeasure.RanKine:
                    resultTemperature = temperatureKelvin * 1.8F;
                    break;

                default:
                    throw new ArgumentOutOfRangeException( "originSystem", originSystem, "Unsupported target system of measure");
            }

            return resultTemperature;
        }

        public static string GetUnitAbbreviation( SystemOfMeasure systemOfMeasure )
        {
            switch( systemOfMeasure )
            {
                case SystemOfMeasure.Fahrenheit:
                    return "°F";

                case SystemOfMeasure.Celsius:
                    return "°C";

                case SystemOfMeasure.Kelvin:
                    return "K";

                case SystemOfMeasure.Reaumer:
                    return "°Re";

                case SystemOfMeasure.RanKine:
                    return "°Ra";

                default:
                    throw new ArgumentOutOfRangeException( "systemOfMeasure", systemOfMeasure, "Unsupported system of measure");
            }
        }

        #endregion Public Methods
    }
}

eHowitzer

 

Published Wednesday, November 24, 2004 5:09 PM by mscomts

Comments

 

null said:

nice
November 25, 2004 12:22 AM
 

Uwe Keim said:

Why do you set "Fahrenheit = 0" in the enum declaration?

I thought a) that this is automatically the first entry and b) that there is no need in your example code to have a specific int value at all?
November 25, 2004 2:20 AM
 

eHowitzer said:

Hi Uwe,

You are right, the default is 0 for the first value in an enumerated type, and the code presented here does not use the integer values in the enumeration.

This class was part of a thermometer control that had some additional UI components that did use the integer values, and I set it just to be explicit.

eHowitzer
November 25, 2004 4:07 PM
New Comments to this post are disabled

This Blog

Syndication

Tags

No tags have been created or used yet.

News

All opinions posted here are those of the author(s) and are in no way intended to represent the opinions of our employer. This is provided "AS IS" with no warranties, and confers no rights. Use of included code samples are subject to the terms specified in the Terms of Use.

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker