Fahrenheit To Celsius Conversion

tempConversion

This is a simple temperature conversion program I came up with. It was for a dept at work that needed an offline program to do simple conversions. I had separated it out into two classes. One for the main program and the other for handling the calculation and storing the private variables.

TemperatureConversion class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TemperatureConversion
{
    public partial class TemperatureConversion : Form
    {
        private Convert _myConversion;

        public TemperatureConversion()
        {
            InitializeComponent();

            //Setting the tool tips for the program.
            ToolTip toolTips = new ToolTip();
            toolTips.SetToolTip(fahrenheitTextBox, "Enter A Temperature To Convert To Celsius.");
            toolTips.SetToolTip(celsiusTextBox, "Enter A Temperature To Convert To Fahrenheit");
            toolTips.SetToolTip(convertButton, "Convert Entered Values");
            toolTips.SetToolTip(exitButton, "Close The Program");
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
            double fahrenheit;
            double celsius;

            double.TryParse(fahrenheitTextBox.Text, out fahrenheit);
            double.TryParse(celsiusTextBox.Text, out celsius);

            if (fahrenheitTextBox.Text.Length > 0 && celsiusTextBox.Text.Length > 0)    //Checking to see if both fields have numbers. If so display a help message and clear the fields.
            {
                MessageBoxInfo("Only Enter A Temperature In One Field At A Time.", "Temperature Conversion");
                fahrenheitTextBox.Clear();
                celsiusTextBox.Clear();
            }
            else if (fahrenheitTextBox.Text.Length > 0)    //If something is in the Fahrenheit field convert it to celsius.
            {
                celsiusConversion(fahrenheit);
            }
            else    //If something is in the celsius field then convert it to fahrenheit.
            {
                fahrenheitConversion(celsius);
            }
        }

        private void celsiusConversion(double farenheitTemperature) 
        {
            _myConversion = new Convert();
            _myConversion.FahrenheitToCelsius(farenheitTemperature);    //Calling the method to convert from Fahrenheit to Celsius.
            Console.WriteLine("Temp In Celsius: " + _myConversion.Celsius);    //Test line to make sure the conversion worked.
            celsiusTextBox.Clear();    //Clearing the text box incase there is something there.
            celsiusTextBox.AppendText(_myConversion.Celsius.ToString());    //Putting the converted value in the Celsius text box.
        }

        private void fahrenheitConversion(double celsiusTemperature)
        {
            _myConversion = new Convert();
            _myConversion.CelsiusToFahrenheit(celsiusTemperature);
            Console.WriteLine("Temp In Fahrenheit: " + _myConversion.Fahrenheit);
            fahrenheitTextBox.Clear();
            fahrenheitTextBox.AppendText(_myConversion.Fahrenheit.ToString());

        }

        public void MessageBoxInfo(string infoDisplay, string infoField)    //Info message method.
        {
            MessageBox.Show(infoDisplay,
                infoField,
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1);
        }
    }
}

Convert Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TemperatureConversion
{
    public class Convert
    {
        private double _fahrenheit;
        private double _celsius;

        public double Fahrenheit
        {
            get { return _fahrenheit; }
            set { _fahrenheit = value; }
        }

        public double Celsius
        {
            get { return _celsius; }
            set { _celsius = value; }
        }

        public double FahrenheitToCelsius(double fahrenheitTemp)    //Method to convert Fahrenheit to Celsius
        {
            return _celsius = fahrenheitTemp / 9 * 5 - 32;
        }

        public double CelsiusToFahrenheit(double celsiusTemp)    //Method to convert Celsius to Fahrenheit
        {
            return _fahrenheit = celsiusTemp / 5 * 9 + 32;
        }
    }
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s