Back to Top

Working with C# Variables


Last time I gave you some homework; you were supposed to make the ResultBox TextBox read-only, thus preventing people from modifying the computed result. Here's what you had to add to the source code:


ResultBox.Enabled = false;


And this is how the modified version of the program was supposed to look:


read onlyNow that we've gotten that out of the way, it's time to start a new Windows Forms project. I have named mine "Variables". Choose a project name, and then press "Create".


Wait until VS creates the project; you will see a blank "Form1" form as soon as everything is set up.


Drag a button to the form, change its "Text" property to "Variables", and then make the form smaller. Here's how my Form1 looks like.


variables1Double click the button to launch its button1_Click event handler.


buttonWe will put all the code that's used in the tutorial inside this method.


private void button1_Click(object sender, EventArgs e)

{

}


Let's begin! A variable is a space in your computer's memory which can be used to store various types of data. C# uses lots of variable types; we will focus on the most important ones in this tutorial.

The first type is the integer, which can store values that range from -2,147,483,648 to 2,147,483,647. If you need to work with numbers that exceed two billion, use "long" variables, which accept numerical values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. I know; that's a very, very big number, so most of us will use regular "int" variables.


It's time for a quick example; type in this line of code inside the private void button1_Click() method:


int firstNumber = 10;

int secondNumber = 20;

int addedNumbers;

addedNumbers = firstNumber + secondNumber;


Build and run the application, and then press the "Variable" button; if everything went okay... nothing will happen! Actually, the program has (hopefully) done its job, adding the two integer variables, but we haven't displayed the addedNumbers variable yet!


Let's have some fun; we will use a Label control to display the result. So, drag and drop a Label to the form, change its "(Name)" to "Result", and then set its text to nothing (an empty string). Add the line below to the button code:


Result.Text = addedNumbers.ToString();


Here's how our method should look like:


source

And here's what happens when we run the program and click the "Variable" button.


resultCool, huh?


Believe it or not, working with float variables is very similar. We need to replace "int" with "float", of course, and we need to add the "f" character at the end of the declaration, like this:


float myVariable = 10f;


If you forget to add the "f", the variable will be treated like a double var, using more memory (64 bits instead of 32 bits). Since any float variable can store values that range from -3.4 x 10^38 to + 3.4 x 10^38, which is much more than what most of us will ever need, you will waste 32 bits of memory anytime you forget to use that "f" character. It's not much, but it adds up when you use lots of variables.


Let's rewrite our button method, making it divide two numbers. Here's the source code that does the job:


private void button1_Click(object sender, EventArgs e)

{

    float firstNumber = 30f;

    float secondNumber = 25f;

    float dividedNumbers;

    dividedNumbers = firstNumber / secondNumber;

    Result.Text = dividedNumbers.ToString();

}


Build the project, run it, and you will see that the proper result is displayed as soon as you click the "Variable" button.


divisionIt's time to move on to the next type: Booleans. These binary variables can have values such as TRUE/FALSE, YES/NO, and so on. Here's how you can define a Boolean variable.


bool itRains = true;

bool itsHot = false;


Let's edit our method once again:


private void button1_Click(object sender, EventArgs e)

{

    bool isEqual;

    isEqual = (1 == 2);

    Result.Text = isEqual.ToString();

}


Replace the code that's used for the button method with the one above; you can copy/paste it to speed up the process.


Can you tell what will happen when you press the "Variable" button? Let's find out!


boolThe code displays "false", because 1 isn't equal with 2, so isEqual will be set to "false". If you use a true statement inside the parentheses, such as (2 == 2), the label will display "true".


Please note that we use "=" to assign values and "==" whenever we compare two different variables, values, etc.


I hope that you've learned quite a few things from this tutorial. I'll see you next time, when we will discuss characters and strings.