Tuesday, July 6, 2010

Programming Introduction lesson 2 - Other Variables

NOTE 1 : Instead od default text on button,you can replace it with "Convert Celsius.." by selecting button1 an going to properties on right and changing Text field of button.
NOTE 2 : You can add labels just for nicer design like I did and change their default text the same way as on button.
NOTE 3 : You can make textBox2 Read-only because we will only need it for output,and we want to pervent users for writing in it.
NOTE 4 : You can change Title by selecting Form(clicking somewhere in form) and going to properties and changing Text Field


In this example Programming Introduction Lesson I will show you how and why can you convert variables of type string,integer and decimal.This simple program converts temperature from Celsius to Fahrenheit.

So we have usual code that IDE generates:


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 Programming_Introduction_Lesson_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/* here goes code
* you will
* writte */
}
}


So we enter designer,and create new button, button1 and 2 Text Boxes,textBox1 and textBox2.
Double click button like in first lesson and designer should create new event for you automatically and take you to code.

So in above example instead of /* here goes code.. you should now have

private void button1_Click(object sender, EventArgs e)
{

}


This part defines what will happen when user press button.
So we define new Integer ( round number),we assume that user will write value in Celsius as Round number,not decimal.So when if we stated int celsius; we would only define it,now we want to put some value (=) in our variable celsius.We want to put value of textBox1 into celsius but we can't do it directly so we need to convert text to round number,and we write

Convert.ToInt32(textBox1.Text); After that we define decimal result; we have to define as decimal because result in fahrenheit is often decimal number,we don't write any value to it because we didnt convert it yet so we just define it for now and later we will assign value to it.

private void button1_Click(object sender, EventArgs e)
{
int celsius = Convert.ToInt32(textBox1.Text);
decimal result;
}

Mathematical formula for Converting celsius to fahrenheit is ((C x 9) /5)+32 = F so we want to simulate exactly that in our program and we write that same formula only C from above is celsius in our program,we need to convert all numbers to decimal,if we don't result will lack decimal places.When result is calculated we want to show user what is result so we need to convert decimal result to string and display it in textBox2.So we write: textBox2.Text = (result).ToString(); Remember to Always put .ToString(); when you are trying to display non string value in label or text box!

private void button1_Click(object sender, EventArgs e)
{
int celsius = Convert.ToInt32(textBox1.Text);
decimal result;

result = ((Convert.ToDecimal(celsius) * Convert.ToDecimal(9)) /
Convert.ToDecimal(5) + Convert.ToDecimal(32));

textBox2.Text = (result).ToString();

}

Upper code is heart of our simple program,Full code is posted below:

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 Programming_Introduction_Lesson_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int celsius = Convert.ToInt32(textBox1.Text);
decimal result;

result = ((Convert.ToDecimal(celsius) * Convert.ToDecimal(9)) /
Convert.ToDecimal(5) + Convert.ToDecimal(32));
textBox2.Text = (result).ToString();

}
}
}

Vote