Building a Fahrenheit/Celcius converter! [Tutorial]

Today we’ll be building outselves a nifty temperature converter! It’s one of the most common tasks a student programmer is asked to make.

Our final solution will look like this:

Farenheit - Celcius Converter

How our final solution will look like.

We’ll use:

  • Labels
  • Textboxes
  • Buttons
  • Class Instancing

Download the final solution here.

The complete tutorial, after the break.

First thing we’ll do is create a class called ConversionFormulas.

Inside this class we’ll create two methods. One to convert from Farenheit to Celcius, and another to convert from Celcius to Farenheit.

Inside your ConversionFormulas class create these methods:

public void ConvertTemperatureFarenheit(TextBox txt1, TextBox txt2)
{
double farenheit = Convert.ToDouble(txt1.Text);
double celcius = (farenheit – 32) * (5.0 / 9.0);
txt2.Text = Convert.ToString(celcius);
}

public void ConvertTemperatureCelcius(TextBox txt1, TextBox txt2)
{
double celcius = Convert.ToDouble(txt2.Text);
double farenheit = (celcius * (9.0 / 5.0)) + 32;
txt1.Text = Convert.ToString(farenheit);
}

Let’s analyze the first line of each method for a bit. When you create a method you can give the method parameters to use. Let me explain what this means:

Let’s say you’re the boss of a sushi chef. You buy him a new knife one day and instruct him on how to gut the fish, cut the fish, scale it, cook it, etc. The chef knows that how to act when he recieves a fish. His instructions were explained by his boss. One day he gets a fish and proceeds to do everything a chef is supposed to do.

It’s a similar situation with methods. You build methods that will work with other controls,methods,etc. Our ConvertTemperatureFarenheit method knows it will recieve 2 textboxes and that’s how we have built it.

You’ll see why we did this in a second.

In your Form1 class create an instance of your ConversionFormulas class:

ConversionFormulas oConversionFormulas = new ConversionFormulas();

Now go to your design tab of Form1 and double click on one button, then double click on the other.

Two onClick method will generate, empty.

private void button1_Click(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{

}

When a user click button1 they want to convert to celcius so let’s call that method:

private void button1_Click(object sender, EventArgs e)
{
oConversionFormulas.ConvertTemperatureFarenheit(textBox1, textBox2);
}

private void button2_Click(object sender, EventArgs e)
{
oConversionFormulas.ConvertTemperatureCelcius(textBox1, textBox2);
}

Notice we first called the object we created of the ConversionFormulas class, then called the methods, then added the parameters we needed; in this case textBox1 and textBox2.

That conclused this tutorial. Hope you enjoyed it.

See ya soon,

Serg

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s