Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 95,475 C# Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 971 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C# Expert

Register to Make This Box Go Away!



C# Temperature Converter

This is a simple Temperature Converter function in C#. It not only converts between Celsius and Fahrenheit but it also does Kelvins conversions as well. I've posted the VB.Net version in the VB.Net Snippets section

Submitted By: PsychoCoder
Actions:
Rating:
Views: 3,238

Language: C#

Last Modified: July 28, 2007

Snippet


  1. enum TempUnits
  2. {
  3.      Kelvins = 0,
  4.      Celsius = 1,
  5.      Fahrenheit = 2
  6. }
  7.  
  8. public double TempConverter(double InitialTemp, TempUnits ConvFrom, TempUnits ConvTo)
  9. {
  10.      double Temp;
  11.      //First make sure they didnt enter same values for convert from & convert to
  12.      if (ConvFrom == ConvTo)
  13.      {
  14.           //Return initial value
  15.           Temp = InitialTemp;
  16.      }
  17.      else
  18.      {
  19.           try {
  20.                //Now decide what they want done
  21.                switch (ConvFrom) {
  22.                     //Convert from Kelvins
  23.                     case TempUnits.Kelvins:
  24.                          //To Celsius
  25.                          if (ConvTo == TempUnits.Celsius)
  26.                          {
  27.                               //Celsius = Kelvin - 273.15
  28.                               Temp = InitialTemp - 273.15;
  29.                          }
  30.                          //To Fahrenheit
  31. else if (ConvTo == TempUnits.Fahrenheit) {
  32.                               Temp = (InitialTemp - 273.15) * 1.8 + 32.0;
  33.                          }
  34.  
  35.                          break;
  36.                     //Convert from Celsius
  37.                     case TempUnits.Celsius:
  38.                          //To Kelvins
  39.                          if (ConvTo == TempUnits.Kelvins)
  40.                          {
  41.                               //Kelvin = Celsius + 273.15
  42.                               Temp = InitialTemp + 273.15;
  43.                          }
  44.                          //To Fahrenheit
  45. else if (ConvTo == TempUnits.Fahrenheit) {
  46.                               //degree F = degree C x 1.8 + 32.
  47.                               Temp = InitialTemp * 1.8 + 32.0;
  48.                          }
  49.  
  50.                          break;
  51.                     //Convert from Fahrenheit
  52.                     case TempUnits.Fahrenheit:
  53.                          //To Celsius
  54.                          if (ConvTo == TempUnits.Celsius)
  55.                          {
  56.                               //degree C = (degree F - 32.) / 1.8
  57.                               Temp = (InitialTemp - 32.0) / 1.8;
  58.                          }
  59.                          //To Kelvins
  60. else if (ConvTo == TempUnits.Kelvins) {
  61.                               Temp = ((InitialTemp - 32.0) / 1.8) + 273.15;
  62.                          }
  63.  
  64.                          break;
  65.                }
  66.           }
  67.           catch (Exception ex) {
  68.                MessageBox.Show(ex.Message, "Conversion Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  69.                Temp = 0;
  70.           }
  71.      }
  72.      return Temp;
  73. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->