Storing data - Variables
What is a variable?
Programs need to be able to store data e.g. one pet name. The simplest way is to use a variable. (If you want to store several pieces of similar data together e.g. ten pet names use an array).
A variable is like a box. You can give it a name, a data type and assign a value. You can then use it later by referring to its name. Find out more here.
How do I create a variable?
A variable should be created as a member of the class. This means you should place it at the top of your file just under the class name.
Type the data type and then the name of the variable. In the example shown here int total will store a whole number in a variable called total. Here the variable has also been assigned an initial value of zero. This is optional. If you don't want to do this, just type
int total;
Basic data types for variables
Here are some of the main data types for variables. Note how data is assigned to each data type. You only need to put the data type when creating the variable. After that you refer to it just by typing its name.
How do I assign a value to a variable?
Assign a value by referring to the variable name (not the data type) like this.
For a whole number (int
data type):
total = 5;
For text (string
data type):
name = "Kerry";
For a single letter (char
data type):
myLetter = 'a';
How do I use the data stored in a variable?
For assignment use the = sign. Refer to the variable by its name only. Here is an example using two variables:
int x;
int y;
x = 9;
y = x;
In the example above two variables are created x
and y
.
The variable x
is then assigned the value 9
.
The variable y
is then assigned to the value stored in x
(which is 9 in this example).
How to I add or subtract using a variable?
We often want to just quickly add to or subtract from a variable. Here is the code to do this efficiently.
To add one, just type ++
after the variable name:
total++;
To subtract one, just type --
after the variable name:
total--;
To add any other value you can either do this:
total = total + 2;
Or you can write even less code by using +=
like this:
total += 2;
You can subtract by using -=
like this:
total -= 2;
Calculating with variables
Use the following symbols are operators to perform basic mathematical calculations.
Example: Multiply two values
In the following example the variable Total is created and set to zero. Total is then assigned a value which is the result of calculating the value stored in the variable Price multiplied by the value stored in Quantity. The new value which is stored in Total is then displayed.
private void btnDisplay_Click(object sender, EventArgs e)
{
// declare a local variables (only just used here)
int Total = 0;
// Calculate total price
Total = Price * Quantity;
MessageBox.Show(Total.ToString());
}
Find out more about calculations and comparisons here.
You can also create and use local variables..
Variables which you only want to use for a specific section of code should be created with that code. In this example the variable Total has been created a local variable. Watch the video below to learn more.
Convert variables from one data type to another
Integer to String use the ToString()
method like this:
int val = 99;
String textVal = val.ToString();
String to Integer. In the example below the string myText
is being converted into an integer:
String myText = "99";
int result = Int32.Parse(myText);
String to Integer. In the example below the string myText
is being converted into an integer:
String myText = "99";
double result = Double.Parse(myText);
Casting
Casting allows you to convert between similar data types. The code below creates an integer variable called val. It then creates a double called newVal. newVal is assigned the value for val. This would cause an error as they are different data types. However (double) casts val into a double.
int val = 99;
double newVal = (double)val;
The difference between String and string
Basic data types shown here just store data. Data types starting with a capital letter are actually classes. They store data and also provide useful extra functions.
Which to use?
If you just want to store data - use the simple data type (e.g. string
). It will take up less memory.
If you want to do more with the data e.g. compare to strings, use the class (e.g. String
).
This is also true for the basic data type int and the class Integer.