Temperature Conversion V2 – Struct Data Types

TemperatureConversionCd

TemperatureConversionMainForm

I wanted to make a change to the Temperature Conversion program I did earlier to demonstrate how Structs work. Structs are a value data type. Unlike a class which is a reference data type. One of the main things I noticed is you don’t need to create an instance to use a struct. However they are limiting which may or may not be good depending on what you want to accomplish.

MainForm 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 MainForm : Form
    {
        private float farenheitValue;                                       //Private variable to hold the farenheightTextBox value.
        private float celsiusValue;                                         //Private variable to hold the celsiusTextBox value.
        private float kelvinValue;                                          //Private variable to hold the kelvinTextBox value.

        public MainForm()
        {
            InitializeComponent();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fahrenheitTextBox_Leave(object sender, EventArgs e)
        {
            //Setting the label to black.
            fahrenheitLabel.ForeColor = Color.Black;

            //Casting the struct.
            TemperatureScales farenheitScales = new TemperatureScales();

            //Attempting to convert the textbox text into a float value.
            if (float.TryParse(fahrenheitTextBox.Text, out farenheitValue))
            {
                //Checking to see if there's text already in the text box.
                if (celsiusTextBox.Text.Length != 0 || kelvinTextBox.Text.Length != 0)
                {
                    //Clearing the text box values.
                    celsiusTextBox.Clear();
                    kelvinTextBox.Clear();

                    //Adding the converted values to the text boxes.
                    celsiusTextBox.Text = farenheitScales.FarenheitToCelsius(farenheitValue).ToString();
                    kelvinTextBox.Text = farenheitScales.FarenheitToKelvin(farenheitValue).ToString();
                }
                //If there isn't anything in the textboxes then proceed with the conversions.
                else
                {
                    //Adding the converted values to the text boxes.
                    celsiusTextBox.Text = farenheitScales.FarenheitToCelsius(farenheitValue).ToString();
                    kelvinTextBox.Text = farenheitScales.FarenheitToKelvin(farenheitValue).ToString();
                }
            }
            //If the conversion cannot happen then the label turns red. This notifies the user something is wrong.
            else
                fahrenheitLabel.ForeColor = Color.Red;
        }

        private void celsiusTextBox_Leave(object sender, EventArgs e)
        {
            //Setting the label to black.
            celsiusLabel.ForeColor = Color.Black;

            //Casting the struct.
            TemperatureScales celsiusScales = new TemperatureScales();

            //Attempting to convert the textbox text into a float value.
            if (float.TryParse(celsiusTextBox.Text, out celsiusValue))
            {
                //Checking to see if there's text already in the text box.
                if (fahrenheitTextBox.Text.Length != 0 || kelvinTextBox.Text.Length != 0)
                {
                    //Clearing the text box values.
                    fahrenheitTextBox.Clear();
                    kelvinTextBox.Clear();

                    //Adding the converted values to the text boxes.
                    fahrenheitTextBox.Text = celsiusScales.CelsiusToFarenheit(celsiusValue).ToString();
                    kelvinTextBox.Text = celsiusScales.CelsiusToKelvin(celsiusValue).ToString();
                }
                //If there isn't anything in the textboxes then proceed with the conversions.
                else
                {
                    //Adding the converted values to the text boxes.
                    fahrenheitTextBox.Text = celsiusScales.CelsiusToFarenheit(celsiusValue).ToString();
                    kelvinTextBox.Text = celsiusScales.CelsiusToKelvin(celsiusValue).ToString();
                }
            }
            //If the conversion cannot happen then the label turns red. This notifies the user something is wrong.
            else
                celsiusLabel.ForeColor = Color.Red;
        }

        private void kelvinTextBox_Leave(object sender, EventArgs e)
        {
            //Setting the label to black.
            kelvinLabel.ForeColor = Color.Black;

            //Casting the struct.
            TemperatureScales kelvinScales = new TemperatureScales();

            //Attempting to convert the textbox text into a float value.
            if (float.TryParse(kelvinTextBox.Text, out kelvinValue))
            {
                //Checking to see if there's text already in the text box.
                if (celsiusTextBox.Text.Length != 0 || fahrenheitTextBox.Text.Length != 0)
                {
                    //Clearing the text box values.
                    celsiusTextBox.Clear();
                    fahrenheitTextBox.Clear();

                    //Adding the converted values to the text boxes.
                    celsiusTextBox.Text = kelvinScales.KelvinToCelsius(kelvinValue).ToString();
                    fahrenheitTextBox.Text = kelvinScales.KelvinToFarenheit(kelvinValue).ToString();
                }
                //If there isn't anything in the textboxes then proceed with the conversions.
                else
                {
                    //Adding the converted values to the text boxes.
                    celsiusTextBox.Text = kelvinScales.KelvinToCelsius(kelvinValue).ToString();
                    fahrenheitTextBox.Text = kelvinScales.KelvinToFarenheit(kelvinValue).ToString();
                }
            }
            //If the conversion cannot happen then the label turns red. This notifies the user something is wrong.
            else
                kelvinLabel.ForeColor = Color.Red;
        }
    }
}

TemperatureScales Struct


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

namespace TemperatureConversion
{
    public struct TemperatureScales
    {
        //Fields
        Farenheit farenheit;
        Celsius celsius;
        Kelvin kelvin;

        //Methods
        //Method to convert from Farenheit to Celsius.
        public float FarenheitToCelsius(float temperature)
        {
            return farenheit.ConvertToCelsius(temperature);
        }

        //Method to covnert from Farenheit to Kelvin.
        public float FarenheitToKelvin(float temperature)
        {
            return farenheit.ConvertToKelvin(temperature);
        }

        //Method to convert from Celsius to Farenheit.
        public float CelsiusToFarenheit(float temperature)
        {
            return celsius.ConvertToFarenheit(temperature);
        }

        //Method to convert from Celsius to Kelvin.
        public float CelsiusToKelvin(float temperature)
        {
            return celsius.ConvertToKelvin(temperature);
        }

        //Method to convert from Kelvin to Farenheit.
        public float KelvinToFarenheit(float temperature)
        {
            return kelvin.ConvertToFarenheit(temperature);
        }

        //Method to convert from Kelvin to Celsius.
        public float KelvinToCelsius(float temperature)
        {
            return kelvin.ConvertToCelsius(temperature);
        }
    }
}

Celsius Struct


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

namespace TemperatureConversion
{
    public struct Celsius
    {
        //Fields
        public float Temperature;

        //Constructor
        public Celsius(float temperature)
        {
            Temperature = temperature;
        }

        //Implicit cast from float to Celsius.
        public static implicit operator Celsius(float temperature)
        {
            return new Celsius(temperature);
        }

        //Method to convert Celsius to Farenheit.
        public float ConvertToFarenheit(float temperature)
        {
            return ((temperature * (9.0F / 5)) + 32);
        }

        //Method to convert Celsius to Kelvin.
        public float ConvertToKelvin(float temperature)
        {
            return (temperature + 273.15F);
        }

        //Method to display Celsius value as a string.
        public override string ToString()
        {
            return Temperature.ToString();
        }
    }
}

Farenheit Struct


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

namespace TemperatureConversion
{
    public struct Farenheit
    {
        //Fields
        public float Temperature;

        //Constructor
        public Farenheit(float temperature)
        {
            Temperature = temperature;
        }
        //Implicit cast from float to Fahrenheit.
        public static implicit operator Farenheit(float temperature)
        {
            return new Farenheit(temperature);
        }

        //Method to convert Farenheit to Celsius.
        public float ConvertToCelsius(float temperature)
        {
            return ((temperature - 32.0F) * (5.0F / 9.0F));
        }

        //Method to convert Farenheit to Kelvin.
        public float ConvertToKelvin(float temperature)
        {
            return ((temperature - 32) * 5 / 9 + 273.15F);
        }

        //Method to display Fahrenheit value as a string.
        public override string ToString()
        {
            return Temperature.ToString();
        }
    }
}

Kelvin Struct


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

namespace TemperatureConversion
{
    public struct Kelvin
    {
        //Fields
        public float Temperature;

        //Constructor
        public Kelvin(float temperature)
        {
            Temperature = temperature;
        }

        //Implicit cast from float to Kelvin.
        public static implicit operator Kelvin(float temperature)
        {
            return new Kelvin(temperature);
        }

        //Method to convert Kelvin to Farenheit.
        public float ConvertToFarenheit(float temperature)
        {
            return ((temperature - 273.15F) * 9 / 5 + 32);
        }

        //Method to convert Kelvin to Celsius
        public float ConvertToCelsius(float temperature)
        {
            return (temperature - 273.15F);
        }

        //Method to display Kelvin value as a string.
        public override string ToString()
        {
            return Temperature.ToString();
        }
    }
}
Advertisements

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