Sunday, 24 November 2013

Ipad Air and Ipad mini 2 review and unboxing




Tuesday, 25 June 2013

Getting Numbers from Text Boxes

We're going to change tack slightly, here. What we'll do is show you how to get numbers from text boxes, and then use these numbers in your code. You'll need to be able to do this for your calculator project, which is coming up soon!
Start a new project for this one by clicking File > New Project from the menu bar at the top of Visual C#.
Add a text box and a button to your new form. Set the following Properties for the text box (the tb below stands for text box):
Name: tbFirstNumber
Size: 50, 20
Location: 40, 35
Text: 10
And set the following properties for your button:
Name: btnAnswer
Size: 75, 25
Location: 90, 90
Text: Answer
Your form will then look like this:
Design this form in C#
What we want to do is to get that number 10 from the text box and display it in a message box.
So double click your button to get at the coding window. Your cursor will be flashing inside of the button code. Set up two integer variables at the top of the button code:
int firstTextBoxNumber;
int answer;
Your coding window should look like this:
Coding Window
To get at the number in the text box, we can use the Text property of text boxes. Here's the line of code to add:
firstTextBoxNumber = tbFirstNumber.Text;
This says, find a text box called tbFirstNumberAccess its Text property. When the Text property is retrieved, store it in the variable called firstTextBoxNumber.
To display the number in a message box, add this line:
MessageBox.Show( firstTextBoxNumber.ToString( ) );
Try to Run your code. You should find C# won't run it at all. It will give you the following error:
An error in our C# code
With text boxes, the thing that you get is, not surprisingly, text. However, we're trying to store the text from the text box into an integer variable. C# won't let you do this - whole numbers belong in integer variables, not text. The error message is telling you that C# can't do the conversion from text to numbers for you - you have to do it yourself!
So we need to convert the text from the text box into an integer. The way you do this is to use something called Parsing. Fortunately, this involves nothing more complex that typing the word "Parse". You can do different types of Parses. Because we need to convert the text into an integer, we need an Integer Parse. So change the line to this:
firstTextBoxNumber = int.Parse( tbFirstNumber.Text );
So you type int, then a full stop. From the IntelliSense menu, you can double click Parse. In between a pair of round brackets, you type the text you want to convert. In our case, the text is coming from a text box. But it doesn't have to. You can do this:
firstTextBoxNumber = int.Parse( "10" );
In the code above, the number is in double quotes. Double quotes mean that it is text. Usingint.Parse( ) means that it will be converted to a number that you can store in an integer variable.
Run your programme and you'll find that it works OK now. (You'll have a green wiggly line underanswer, but that's just because we haven't used this variable yet.) Click your button and the number 10 will appear in the message box. Type a different number in your text box, and click the button again. The new number should appear in place of the old one.
You can also Parse other types of variable. Like this:
float firstTextBoxNumber;
firstTextBoxNumber = float.Parse( tbFirstNumber.Text );
Or this:
double firstTextBoxNumber
firstTextBoxNumber = double.Parse( tbFirstNumber.Text );
In the first one, we've set up a float variable. We've then used float.Parse( ) to convert the text from the text box, so that it can be stored in the float variable. We've done exactly the same thing in the second example, to convert the text into a double.
Things get more complicated if you accidentally try to store a double value in a float variable - your programme will crash! You need to try to catch things like this with code. (You'll see how to test for errors like this later in the book.)
For now, let's move on.
OK, so we've got text from a text box and displayed it in a message box. What we'll do now is to add a second text box, get numbers from both, use our Math operators, and do some calculations with the two number we took from the text boxes. Sounds complex, but it isn't!
Add a second text box to your form. Set the following Properties for it in the Properties Window:
Name: tbSecondNumber
Size: 50, 20
Location: 165, 35
Text: 5
Your form will then look like this:
Add a new text box to your form
Double click the button to get at your code. Now set up another integer variable to hold the second number from the new text box:
int secondTextBoxNumber;
To store the number from the text box in this new variable, add the following line:
secondTextBoxNumber = int.Parse( tbSecondNumber.Text );
This is the same as before - use int.Parse to convert the number from the text box into an integer variable. Then store the number in the new variable.
Let's add the two numbers up, first. We can use the answer variable for this. Here's the code to add:
answer = firstTextBoxNumber + secondTextBoxNumber;
So we're just using the plus symbol ( + ) to add up whatever is in the two variables. The numbers in the variables come from the two text boxes.
Amend your message box line to this:
MessageBox.Show( answer.ToString( ) );
All you need to do is to change the name of the variable that your are converting ToString( ).
Your coding window should look like ours:
Get 2 numbers from the text boxes
Run your programme, and then click the button. You should the answer to the addition in your message box:
The answer is displayed in a message box
Change the numbers in the text boxes, and click your button again. When you've finished playing with your new form, click the red X to return to the code. Here are a few exercises for you to try.

Exercise
Use the textboxes on your form to calculate the following (you'll need to amend your code for three of them):
1845 + 2858
3450 - 285
35 * 85
5656 / 7
(The answers you should get are: 4703, 3165, 2975 and 808.)

Exercise
Add a new text box to you form. Set up an integer variable to store a third number. Get the third number from the text box and calculate the following:
(1845 + 2858) - 356
(3450 - 285) * 12
35 * ( 85 - 8 )
(5656 / 7) + 2156
(The answers you should get are: 4347, 37980, 2695 and 2964. You'll have to keep closing the form down. Then add round brackets, the operator symbols, and the new variable.)

Once you've completed the exercises, you can move on to tackling the next project - your very own calculator!

Multiplication and Division in C#

To multiply and divide, the following symbols are used in C# programming:
* Multiply
/ Divide
Change your code to this:
answer = ( firstNumber + secondNumber ) * thirdNumber;
Because of the brackets, the first thing that C# does is to add the value in firstNumber to the value insecondNumber. The total is then multiplied ( * ) by the value in thirdNumber. With the values we currently have in the variables, the sum is this:
answer = ( 100 + 75 ) * 50
Run your programme and click your button. The answer you should get is 8750. Return to the coding window. Now remove the two round brackets. Your coding window will then be this:
Multiply and divide
Run your programme again. Click the button. This time, the answer is 3850! The reason it's different is because of Operator Precedence. With the brackets, you forced C# to calculate the addition first. Without the brackets, C# no longer calculates from left to right. So it's NOT doing this:
( 100 + 75 ) * 50
C# sees multiplication as having priority over addition and subtraction. So it does the multiplying first. It does this:
100 + ( 75 * 50 )
The two give you totally different answers.
The same is true of division. Try this. Amend your line of code to the following:
answer = ( firstNumber + secondNumber ) thirdNumber;
Run the programme and the answer will be 3. (The real answer to (100 + 75) / 50 is, of course, 3.5. But because we're using integer variables and not floats, the point something at the end gets chopped off.)
So we're using the divide symbol now ( / ), instead of the multiplication symbol (*). The addition is done first, because we have a pair of round brackets. The total of the addition is then divided by the value in thirdNumber.
Return to the code, and change the line to this:
answer = firstNumber + secondNumber / thirdNumber;
So just remove the round brackets. Run your programme again, and you'll find that the answer in the message box is now 101. (It would have been 101.5, if we had used a float variables instead of integers.)
If you now replace the plus symbol ( + ) above with a multiplication symbol ( * ), C# switches back to "left to right" calculation. This is because it sees division and multiplication as having equal weight. The answer you'll get without brackets is 150.
Exercise B
Try the following two lines. First this one
answer = (firstNumber * secondNumber) / thirdNumber;
And now this one:
answer = firstNumber * (secondNumber / thirdNumber);
What answer do you get with the round brackets in different places? Can you understand why? If not, go back over this section.

Operator Precedence in C# .NET

The symbols you have been using ( + and - ) are known as Operators. Operator Precedence refers to the order in which things are calculated. C# sees the plus (+) and the minus ( - ) as being of equal weight, so it does the calculating from left to right. But this "left to right" calculating can cause you problems. Change the plus into a minus in your code, and the minus into a plus. So change it to this:
answer = firstNumber - secondNumber + thirdNumber;
Run your code. When the button is clicked, an answer of 75 will appear in the message box, instead of the 125 you got last time! This is "left to right" calculation. What you wanted here was:
firstNumber - secondNumber
Then when the answer to that is found, add the thirdNumber. So the sum is this: 100 - 75, which equals 25. Then 25 + 50, which equals 75.
But what if you didn't mean that? What if you wanted firstNumber minus the answer tosecondNumber + thirdNumber? In case that's not clear, some brackets may help clear things up. Here's the two ways of looking at our calculation:
( firstNumber - secondNumber ) + thirdNumber
firstNumber - ( secondNumber + thirdNumber )
In maths, brackets are a way to clear up your calculations. In the first one, whatever is between the round brackets is calculated first. The total of the sum in brackets is then added to thirdNumber. In the second one, it's the other way around: secondNumber is first added to thirdNumber. You then deduct from this total the value of firstNumber.
You can use brackets in programming, as well. Add the following brackets to your code:
answer = firstNumber - (secondNumber + thirdNumber);
Your coding window should then look like this:
Round brackets have been added to the C# code
When you run your programme and click the button, this time the answer is minus 25. Previously, the answer was 75! The reason the answer is different is because you've used brackets. C# sees the brackets and tackles this problem first. The next thing it does is to deduct firstNumber from the total. Without the brackets, it simply calculates from left to right.

Mixing Subtraction and Additionin C#

You can mix subtraction and addition. The process is quite straightforward. What we'll do next is to add two numbers together, and then subtract a third number from the total.
Add another button to your form. Set the following properties for it:
Name: btnMixed
Size: 100, 30
Text: Add and Subtract
(If you need to make your Form bigger, click on the form to select it. Then change the Size property in the Properties Window.)
Double click your button to get at the code. We'll need four integer variables for this. So set up the following:
int firstNumber;
int secondNumber;
int thirdNumber;
int answer;
To place values in the variables, add the following three lines:
firstNumber = 100;
secondNumber = 75;
thirdNumber = 50;
Your coding window will then look like this:
Three integer variables
To add the first number to the second number, and then place the result in the variable we've calledanswer, add the following line to your code:
answer = firstNumber + secondNumber;
Display the answer in a message box by adding this line:
MessageBox.Show( answer.ToString( ) );
When you run the programme and click your button, you should see the message box display an answer of 175.
Stop the programme and return to your code.
We now want to subtract the third number from the first two. So change this line:
answer = firstNumber + secondNumber;
to this:
answer = firstNumber + secondNumber - thirdNumber;
When C# sees all these variables after the equals sign, it will try to calculate using the numbers you've stored in the variables. Usually, it will calculate from left to right. So this bit gets done first:
firstNumber + secondNumber
When C# finishes adding the first two numbers, it will then deduct the value in thirdNumber. The answer is then stored in the variable to the left of the equals sign.
Run your programme. When the button is clicked, the message box will display an answer of 125.
So mixing addition and subtraction is fairly straightfoward - just use the + and - symbols. However, there can be problems! In the next part, you'll about something called Operator Precedence.

Friday, 21 June 2013

The fastest ram of your gaming pc

Corsair Dominator GT CMGTX8

The only premium memory kit in today's round-up containing four 2 GB modules, Corsair's 8 GB DDR3-2400 kit promises top overclocking potential. These two things are probably related, since memory controllers are often capable of being pushed a little harder when paired with lower-density RAM.
As with other Dominator GT memory, the CMGTX8 kit features removable fins and Corsair's DHX Pro connector on each module. The fins can be replaced by parts of alternative size and color or, if you can still find one, the firm's long-discontinued thermoelectric cooler. The DHX Pro connector is designed exclusively to support Corsair's AirFlow Pro temperature and activity display.
Rated at DDR3-2400 CAS 10-12-10-27, the CMGTX8 booted at a mere DDR3-1333 CAS 9 on our Asus P9X79 WS test motherboard. A quick look at the SPD table reveals the reason.
Corsair skipped the now-standard DDR3-1600 C9 defaults in what appears to be an effort to assure the ultimate compatibility, since the lower speed will almost assuredly boot on nearlyany board. Data rates lower than DDR3-1066 are no longer needed, since Sandy Bridge-E supplants the only processor architecture left officially constrained to that speed, Gulftown. We're not sure which platforms will treat the 518 MHz value as DDR3-1066, though.
CPU-Z doesn't report the correct frequency for XMP-2400, but our motherboard read it without issue and set the appropriate timings automatically when switched to XMP Profile 1 in its UEFI.
Corsair DRAM carries a non-transferable limited lifetime warranty.

Subtraction in C# .NET

Subtraction is fairly straightforward in C#. To subtract one number from another, you use the minus symbol ( - ).
Add another button to your form. Set the following properties for it in the Properties Window:
Name: btnSubtract
Size: 100, 30>
Text: Subtract
Double click the button to get at the code. Add the following three lines of code:
int answerSubtract;
answerSubtract = 50 - 25;
MessageBox.Show( answerSubtract.ToString() );
Your coding window will then look like this:
Subtraction code in C# .NET
So we've set up an integer variable called answerSubtract. On the second line, we're using the minus symbol to subtract 25 from 50. When C# works out the answer to 50 - 25, it will place the answer to the left of the equals sign, in the answerSubtract variable. On the final line, we're displaying the answer in a message box.
Run your code, and make sure it works. The answer you should see in the message box is, of course, 25. Stop your programme and return to the coding window. Now change the 25 to 25.5.
answerSubtract = 50 - 25.5;
Try to run your programme and you'll get the blue wiggly line, meaning there's an error in your code. The reason is the same as for addition: we're trying to place a float number into an integer variable (the answer will be 24.5, this time). Just because the math symbol has changed doesn't mean we can disobey the C# rules!
Change it back to 25, and the code will run fine.
As with addition, you can subtract more than one number. Change your line to this:
answerSubtract = 50 - 25 - 10 - 2;
When you run your programme, you should see 13 in your message box, the answer to the subtraction.
You can also use variable names in your subtraction. Add the following integer variable to your code:
int numberOne = 12;
Then change the second line to this:
answerSubtract = 50 - numberOne;
Here's what your coding window should look like:
A new variable has been added
What we're doing here is setting up an integer variable called numberOne. We're then placing a value of 12 inside of the variable - all on the same line. For the second line, we're subtracting whatever is in the variable called numberOne from 50.
Run you programme and click your button. You should see an answer of 38 in your message box.
Exercise A
Set up another variable. Call it numberTwo. Place a value of 4 inside of this new variable. Subtract from 50 the value in numberOne and the value in numberTwo. When you run your code, the answer you get in the message box should be 34.

Adding up with float Variables

You add up with float variables in exactly the same way - with the plus symbol. You can even mix integer variables with float variables. But you have to take care!
Add another button to your form, and set the following properties for it in the Properties Window:
Name: btnAddFloats
Size: 100, 30
Text: Float - Add
Double click your button to get at the code. Set up the following variables:
float firstNumber;
float secondNumber;
float floatAnswer;
And here's the coding window:
Three floats variables have been declared
(Notice that we've used the same names for the first two variables. C# doesn't get confused, because they are in between the curly brackets of the button code. You can set up variables outside of the curly brackets. We'll do this when we come to code the calculator, at the end of this section. Then something called scope comes in to play. Don't worry about it, for now.)
To place something in your new variables, add the following code:
firstNumber = 10.5F;
secondNumber = 32.5F;
floatAnswer = firstNumber + secondNumber;
Finally, add you message box line:
MessageBox.Show( floatAnswer.ToString( ) );
The coding window should look like this:
The whole code
Run your form and click your new button. You should see this:
The form displaying the answer in a message box
So 10.5 + 32.5 equals 43. Halt your form by clicking the red X, and return to your coding window.
As was mentioned, you can add float and integer values together. But you need to take care. Try this:
Add the following variable to your code:
int integerAnswer;
And then change this line:
floatAnswer = firstNumber + secondNumber;
To this:
integerAnswer = firstNumber + secondNumber;
So it's just the name of the variable before the equals sign that needs to be changed.
Amend you message box line from this:
MessageBox.Show( floatAnswer.ToString( ) );
To this:
MessageBox.Show( integerAnswer.ToString() );
Your coding window will then look like this:
The code has been changed
Try to run your code. The programme won't execute, and you'll have a blue wiggly line:
A build error in C#
Hold your mouse over the blue wiggly line and you'll see an explanation of the error:
C# error message
Not much help, if you're a beginner! But what it's telling you is that the first number and the second number are float variables. The answer to the addition was also a float. However, you were trying to store the answer in an integer variable. C# won't let you store float values in an integer. The error message is saying that you need to convert them first.
You can indeed convert float values to integers. You do it like this:
integerAnswer = (int) firstNumber + (int) secondNumber;
So you type the word int between a pair of round brackets. This goes before the number you want to convert. It does mean that the point something on the end will get chopped off, though. So 10.5 becomes 10, and 32.5 becomes 32. Not good for accuracy, but at least the programme will run!
Try it out, and you should see an answer of 42 when you click your button.
So the moral is this: If you're expecting an answer that ends in point something, use a float variable (or a double).
(You may have a green wiggly line under float floatAnswer. This is because you're not storing anything in this variable. Don't worry about it!)
Note that the other way round is not a problem - you can store an integer in a float value. Have a look at this slight change to the code:
Some new C# code
First, notice the new way we are storing the number 20 into the integer variable calledintegerAnswer:
int integerAnswer = 20;
Instead of two lines, we've just used one. This is fine, in C#. But you're doing two things on the same line: setting up the variable, and placing a value in it.
The second thing to notice is that we are adding up two float values (firstNumber and secondNumber) and an integer (integerAnswer). We're then storing the answer into a float variable (floatAnswer). Try it out and you'll find that the code runs fine.
If we change this line:
firstNumber = 10.5F;
to this:
firstNumber = 10;
then, again, the programme will run fine. In other words, you can store an integer in a float variable, but you can't store a float value in an integer variable without converting.
Hopefully, that wasn't too confusing!
We'll move on to subtraction, now. But if you want to use a double variable instead of a float variable the same things apply - be careful of what you are trying to store, and where!

Addition in C# .NET

We'll now use variables to do some adding up. After you have learned how to add up with the three number variable types, we can move on to subtraction, multiplication, and division.
Start a new project for this. So click File > Close Solution from the menu bar at the top of Visual C#. Then click File > New Project. Type arithmetic as the Name of your new Windows FormsApplication project.Click OK to create the new project.
Add a button to your new form, and set the following properties for it in the Properties Window:
Name: btnAdd
Size: 100, 30
Text: Integer - Add
Move the button to the top of your form. Then double click it to get at the coding window. Set up the following three integer variables in your button code:
int firstNumber;
int secondNumber;
int integerAnswer;
Your coding window should look like ours below:
Three integers have been set up
We now need to put something into these variables. We'll store 10 in the first number, and 32 in the second number. So add these two lines to your code:
firstNumber = 10;
secondNumber = 32;
Your coding window will then look like this:
Assign values to the integers
So the numbers we want to store in the variables go on the right hand side of the equals sign; the variable names go on the left hand side of the equals sign. This assigns the numbers to the variables - puts them into storage.
We now want to add the first number to the second number. The result will be stored in the variable we've called integerAnswer. Fortunately, C# uses the plus symbol (+) to add up. So it's fairly simple. Add this line to your code:
integerAnswer = firstNumber + secondNumber;
And here's the coding window:
Adding the values in two variables
We've already stored the number 10 in the variable called firstNumber. We've stored 32 in the variable secondNumber. So we can use the variable names to add up. The two variables are separated by the plus symbol. This is enough to tell C# to add up the values in the two variables. The result of the addition then gets stored to the left of the equals sign, in the variable calledintegerAnswer. Think of it like this:
C# adds up the values to the right of the = sign
Calculate this sum first

The answer is stored to the left of the = sign
Store the answer here

To see if all this works or not, add a message box as the final line of code:
MessageBox.ShowintegerAnswer.ToString( ) );
We're just placing the integerAnswer variable between the round brackets of Show( ). Because it's a number, we've had to use ToString( ) to convert the number to text. Here's what your coding window should look like now:
The whole code
And here's the form when the button is clicked:
The message box displays the answer to the addition
You don't have to store numbers in variables, if you want to calculate things. You can just add up the numbers themselves. Like this:
integerAnswer = 10 + 32;
And even this:
integerAnswer = firstNumber + 32;
So you can add up just using numbers, or you can mix variable names with numbers. As long as C# knows that there's a number in your variable, and that it's the right type, the addition will work.
You can use more than two variables, or more than two numbers. So you can do this:
integerAnswer = firstNumber + secondNumber + thirdNumber;
or this:
integerAnswer = firstNumber + secondNumber + 32;
And this:
integerAnswer = firstNumber + 10 + 32;
The results is the same: C# adds up whatever you have on the right hand side of the equals sign, and then stores the answer on the left hand side.
In the next part, you'll see how to add up with float variables.

Double Variables in C# .NET

Add another button to your form, and set the following properties for it in the Properties Window:
Name: btnDouble
Location: 110, 130
Text: Double
Double click your new button to get at the code. Add the following three lines to your button code:
double myDouble;
myDouble = 0.007;
MessageBox.Show(myDouble.ToString());
Your coding window should now look like this:
Your coding window
Run your programme and click your new button. You should see this:
You also need to be careful of precision when using double variable types. The double type can hold up to 16 digits.
Halt your programme and return to the coding window. Change this line:
myDouble = 0.007;
to this:
myDouble = 12345678.1234567;
Run your programme and click your double button. The message box correctly displays the number. Add another number on the end, though, and C# will again round up or down. The moral is, if you want accuracy, careful of rounding!
In the next part, you'll see how to add up in C#.

Double and Float Variables in C#

Integers, as was mentioned, are whole numbers. They can't store the point something, like .7, .42, and .007. If you need to store numbers that are not whole numbers, you need a different type of variable. You can use the double type, or the float type. You set these types of variables up in exactly the same way: instead of using the word int, you type double, or float. Like this:
float myFloat;
double myDouble;
(Float is short for "floating point", and just means a number with a point something on the end.)
The difference between the two is in the size of the numbers that they can hold. For float, you can have up to 7 digits in your number. For doubles, you can have up to 16 digits. To be more precise, here's the official size:
float: 1.5 × 10-45 to 3.4 × 1038
double: 5.0 × 10-324 to 1.7 × 10308
Float is a 32-bit number and double is a 64-bit number.
To get some practice using floats and doubles, return to your form. If you can't see the Form1.cs [Design] tab at the top, right click Form1.cs in the Solution Explorer on the right hand side. (If you can't see the Solution Explorer, click View > Solution Explorer from the menu bar at the top.)
View Designer
Add a new button to your form. Set the following properties for it in the Properties Window:
Name btnFloat
Location: 110, 75
Text: Float
Double click your new button, and add the following line to the button code:
float myFloat;
Your coding window will then look like this:
A float variable in C# .NET
To store something inside of your new variable, add the following line:
myFloat = 0.42F;
The capital letter F on the end means Float. You can leave it off, but C# then treats it like a double. Because you've set the variable up as a float, you'll get errors if you try to assign a double to a float variable.
Add a third line of code to display your floating point number in a message box:
MessageBox.Show( myFloat.ToString( ) );
Again, we have to use ToString( ) in order to convert the number to a string of text, so that the message box can display it.
But your coding window should look like ours below:
C# Button Code
Run your programme and click your Float button. You should see a form like this:
The Float variable is displayed in the Message Box
Halt the programme and return to your coding window. Now delete the capital letter F from 0.42. The line will then be:
myFloat = 0.42;
Try to run your programme again. You'll get an error message, and a blue wiggly line under your code. Because you've missed the F out, C# has defaulted to using a double value for your number. A float variable can't hold a double value, confirming that C# is a strongly typed language. (The opposite is a weakly typed language. PHP, and JavaScript are examples of weakly typed languages - you can store any kind of values in the variables you set up.)
Another thing to be careful of when using float variables is rounding up or down. As an example, change the number from 0.42F to 1234.567F. Now run your programme, and click your float button. The message box will be this:
Halt the programme and return to your code. Now add an 8 before the F and after the 7, so that your line of code reads:
myFloat = 1234.5678F;
Now run your programme again. When you click the button, your message box will be this:
Rounding Up
It's missed the 7 out! The reason for this is that float variables can only hold 7 numbers in total. If there's more than this, C# will round up or down. A number that ends in 5 or more will be rounded up. A number ends in 5 or less will be rounded down:
1234.5678 (eight numbers ending in 8 - round up)
1234.5674 (eight numbers ending in 4 - round down)
The number of digits that a variable can hold is known as precision. For float variable, C# is precise to 7 digits: anything more and the number is rounded off.

Thursday, 20 June 2013

Numbers Variables in C# .NET


s well as storing text in memory you can, of course, store numbers. There are a number of ways to store numbers, and the ones you'll learn about now are called Integer, Double and Float. First up, though, are Integer variables.
First, close any solution you have open by clicking File > Close Solution from the menu bar at the top of Visual Studio. Start a new project by clicking File > New Project. From the New Project dialogue box, select Windows Forms Application from the available templates. Type a Name for your project. Call it Numbers.
Click OK, and you'll have a new form to work with.

C# Integers

An integer is a whole number. It's the 6 of 6.5, for example. In programming, you'll work with integers a lot. But they are just variables that you store in memory and want to manipulate. You'll now see how to set up and use Integer variables.
Add a button to your form, and set the following properties for it in the Properties Window:
Name: btnIntegers
Text: Integers
Location: 110, 20
Now double click your button to get at the code:
Code for the Button
In the previous section, you saw that to set up a string variable you just did this:
string myText;
You set up an integer variable in the same way. Except, instead of typing the word string, you type the word int (short for integer).
So, in between the curly brackets of your button code, type int. You should see the word turn blue, and the IntelliSense list appear:
IntelliSense list
Either press the enter key on your keyboard, or just hit the spacebar. Then type a name for your new variable. Call it myInteger. Add the semi-colon at the end of your line of code, and hit the enter key. Your coding window will then look like this:
An Integer variable in C#
Notice the text in the yellow box, in the image one up from the one above. It says:
Represents a 32-bit signed integer
A signed integer is one that can have negative values, like -5, -6, etc. (The opposite, no negative numbers, is called an unsigned integer.) The 32-bit part is referring to the range of numbers that an integer can hold. The maximum value that you can store in an integer is: 2,147,483,648. The minimum value is the same, but with a minus sign on the front: -2,147,483,648.
To store an integer number in your variable, you do the same as you did for string: type the name of your variable, then an equals sign ( = ), then the number you want to store. So add this line to your code (don't forget the semi-colon on the end):
myInteger = 25;
Your coding window should look like this:
A value assigned to a C# integer variable
So we've set up an integer variable called myInteger. On the second line, we're storing a value of 25 inside of the variable.
We'll use a message box to display the result when the button is clicked. So add this line of code for line three:
MessageBox.Show(myInteger);
Now try to run your code. You'll get the following error message:
Build Errors message box, C# 2012
You should see a blue wiggly line under your MessageBox code:
The error has been highlighted
Hold your mouse over myInteger, between the round brackets of Show( ). You should see the following yellow box:
Error Explanation
The error is: "Cannot convert from int to string". The reason you get this error is because myIntegerholds a number. But the MessageBox only displays text. C# does not convert the number to text for you. It doesn't do this because C# is a programming language known as "strongly typed". What this means is that you have to declare the type of variable you are using (string, integer, double). C# will then check to make sure that there are no numbers trying to pass themselves off as strings, or any text trying to pass itself off as a number. In our code above, we're trying to pass myInteger off as a string. And C# has spotted it!
What you have to do is to convert one type of variable to another. You can convert a number into a string quite easily. Type a full stop (period) after the "r" of myInteger. You'll see the IntelliSense list appear:
Convert ToString Method
Select ToString from the list. Because ToString is a method, you need to type a pair of round brackets after the "g" of ToString. Your code will then look like this (we've highlighted the new addition):
The ToString Method
The ToString method, as its name suggests, converts something to a string of text. The thing we are converting is an integer.
Start your programme again. Because you've converted an integer to a string, you should find that it runs OK now. Click your button and you should see the message box appear:
In the next lesson, we'll take a look at double variables, and float variables.

Comments in C# .NET

You don't have to use a message box to display the result. You can use other controls, like a Label. Let's try it.
Add a new Label to your form. Use the Properties Window to set the following properties for your new Label:
Name: TextMessage
Location: 87, 126
Text: Message Area
Return to your coding window, and add two forward slashes to the start of your MessageBox.Show( ) line. The line should turn green, as in the following image:
A Commented line
The reason it turns green is that two forward slashes are the characters you use to add a comment. C# then ignores theses lines when running the programme. Comments are a very useful way to remind yourself what the programme does, or what a particular part of your code is for. Here's our coding window with some comments added:
Comments added to C# Code
You can also use the menu bar, or the toolbar, to add comments. Highlight any line of text in your code. From the menu bar at the top of Visual C#, select Edit > Advanced > Comment Selection. Two forward slashes will be added to the start of the line. You can quickly add or remove comments by using the toolbar. Locate the following icons on the toolbars at the top of Visual C#:
The Comment icons on the Visual C# toolbar
In version 2012, the comment icons look like this:
The Comment icons on the Visual C# toolbar, version 2012
The comment icons are circled in red, in the images above. The first one adds a comment, and the second one removes a comment. (If you can't see the above icons anywhere on your toolbars, clickView > Toolbars > Text Editor.)
Now that you have commented out the MessageBox line, it won't get executed when your code runs. Instead, add the following like to the end of your code:
TextMessage.Text = messageText + firstName;
Your coding window should then look like this:
Run your programme again. Type your name in the text box, and then click your button. The message should now appear on your label, instead of in a Message Box:
The message appears on the label
The reason is does so is because you're now setting the Text property of the Label with code. Previously, you changed the Label's Text Property from the Properties Window. The name of our label is TextMessage. To the right of the equals sign, we have the same code that was in between the round brackets of the Show( ) method of the MessageBox.
OK, time for an exercise.
Exercise
Add a second text box to your form. Display your message in the text box as well as on the label. So if your name is John, your second text box should have: "Your name is: John" in it after the button is clicked.
When you complete this exercise, your form should look like this, when the button is clicked:
We’re now going to move away from string variables and on to number variables. The same principles you’ve just learnt still apply, though:
  • Set up a variable, and give it a name
  • Store something in the variable
  • Use code to manipulate what you have stored