Before beginning this is for the absolute beginners. Because we all know at first coding can be a little daunting. Most people (although i picked it up right off the bat) have issues just grasping the basic if statement system. So this is a quick overview to get them started up on that and programming. In this guide i will be using C# (pronounced C Sharp), a very powerful language with a managed environment. Just as fast as C++ with the exception of speed lost due to it being a managed environment but you can get 10 times the work done in the same amount of time (not literally of course but you get the point.) Keywords such as if are colorized, pay attention to these words as i have done this for a reason.
First off, download and install Microsoft Visual Studio 2008 Express Edition C#. This is a free compiler made by Microsoft for C#.Net. Accompanied will be the .Net framework either version 2.0 or 3.5 (pending your OS.) I wouldn't recommend downloading the MSDN Library since its only a small section of information that is rarely used and adds a large amount of time to the setup. Additionally i want to explain the color coating. Light blue is for data types such as strings, integers, or doubles. Blue (regular) is used for statements, such as : if, switch, else. Light green is used for various other things like project names.
Part 1 : Hello World! Applications
If you are using Visual Studio now, heres instructions to create a form with a button to execute our code. Open Visual Studio and click create new project. Then select windows forms application. Once thats done open your toolbox and drag a button onto the form. Right click and click properties if you want do edit anything about the button, such as text. Double click the button to bring up the button click event (an event is a section of code executed when something happens.) To achieve this we call the MessageBox class. And the Show function. For arguments we enter the string "Hello World!" Take note that all strings that are not variables must be enclosed in double quotes ("".)
Code:
To begin explaining this Messagebox.Show is a functions. The value inside parenthesis is called an argument. In this case the argument is a string with the value "Hello World!" Put together this code displays a message box with the test "Hello World!" in it and an ok button to close it. (On an advanced note, this function locks the thread it is executed in until the OK button is clicked.) At the end a semicolon ( ; ) is placed, this indicates the end of a line.
Part 2 : Variables, Data Types, && Specifics
Above we used a string as an argument. A string is one of the 13 data types in C#. The most common data types used are : strings, integers (sizes 8 - 64 bits), doubles, decimals, booleans and floats. The following is a list of these, and what types of information they can store.
Code:
Continuing from that. If we wanted to say, multiply a number's current value by a number we would use *=. If we wanted to subtract 2 from a variables current value we would use -=. Some others include /= (division), += (addition), and a few others that aren't as important to cover for now. Heres an example of this.
Code:
Part 3 : The Statements - if, switch, and else.
This is where usually people have issues understanding when i try to teach them how to program. I often start here but decided not to for this FAQ. Lets start with if statements. These statements execute code in brackets only if their requirements are true. The following example only executes code if a boolean is equal to true.
Code:
Additionally, what if we want code to also execute in the event the requirements for the if statement are not met? For this we use an else statement.
Code:
What if the value may have multiple answers, but we wont want to write all those if statements needed? We would use a switch statement. The switch statement changed the code to be executed based on the value of the variable in parenthesis.
Code:
As you saw above, switch is followed by the value the statement is dealing with in parenthesis. And case is followed by a value in parenthesis. If the value the switch statement is referring to is the same as a value inside the parenthesis next to the case statement then the code of that case statement is executed.
First off, download and install Microsoft Visual Studio 2008 Express Edition C#. This is a free compiler made by Microsoft for C#.Net. Accompanied will be the .Net framework either version 2.0 or 3.5 (pending your OS.) I wouldn't recommend downloading the MSDN Library since its only a small section of information that is rarely used and adds a large amount of time to the setup. Additionally i want to explain the color coating. Light blue is for data types such as strings, integers, or doubles. Blue (regular) is used for statements, such as : if, switch, else. Light green is used for various other things like project names.
Part 1 : Hello World! Applications
If you are using Visual Studio now, heres instructions to create a form with a button to execute our code. Open Visual Studio and click create new project. Then select windows forms application. Once thats done open your toolbox and drag a button onto the form. Right click and click properties if you want do edit anything about the button, such as text. Double click the button to bring up the button click event (an event is a section of code executed when something happens.) To achieve this we call the MessageBox class. And the Show function. For arguments we enter the string "Hello World!" Take note that all strings that are not variables must be enclosed in double quotes ("".)
Code:
Code:
MessageBox.Show("Hello World!");
Part 2 : Variables, Data Types, && Specifics
Above we used a string as an argument. A string is one of the 13 data types in C#. The most common data types used are : strings, integers (sizes 8 - 64 bits), doubles, decimals, booleans and floats. The following is a list of these, and what types of information they can store.
- Strings : Contains Unicode characters.
- Integers (32bit) : -2,147,483,647 to +2,147,483,647. Referred to as Int32. No decimal places.
- Doubles : â€"5.0x10-324 to +1.7x10308.
- Decimals : +1.0x10-28 to +7.9x1028.
- Booleans : 0 or 1, False or True.
- Floats : â€"1.5x10-45 to +3.4x1038. Referred to as Single.
Code:
Code:
String mystring = "This is the value of the string, mystring is the name of the variable.";
Int32 myint = 378; //This is a 32bit integer
Double mydouble = 378.0001; //This is a double, unlike the integer it can store decimals, it is slower then an integer. It is a member of the floating point family.
Decimal mydecimal = 1000.873; //This is a Decimal
Boolean mybool = False; //This is a boolean, it can be only true or false. It accepts no other value.
Single myfloat = 2008.29; //This is a floating point.
mydouble = 64.3 * myint;
/*This sets mydouble to 64 times the value of myint.
In short, mydouble is now equal to 24305.4.
We used a double because it can store decimal places necessary for this value.
The / followed by a * surrounding this text is a way to
comment out text of multiple lines.*/
Code:
Code:
Int32 myint; //Initializes the variable, note it was done without setting the variable this time to show you this can be done as well.
myint = 10;
myint *= 10; //Now myint is equal to 100
This is where usually people have issues understanding when i try to teach them how to program. I often start here but decided not to for this FAQ. Lets start with if statements. These statements execute code in brackets only if their requirements are true. The following example only executes code if a boolean is equal to true.
Code:
Code:
Boolean mybool = true;
if (mybool == true) //= is used when settings values, == is used when checking values.
{
MessageBox.Show("This message box is showing because the boolean mybool is equal to true, and thus the requirements of this if statement are met.");
}
Code:
Code:
Boolean mybool = false;
if (mybool == true)
{
MessageBox.Show("mybool is equal to true");
}
else
{
MessageBox.Show("mybool is equal to false");
}
Code:
Code:
int32 myint = 2;
switch(myint) //This specifies we are talking about the value myint.
{
case(0) :
MessageBox.Show("myint is equal to 0");
break;
case(1) : MessageBox.Show("we can also format it this way"); break;
case(2) : MessageBox.Show("this is what will execute since myint is equal to 2"); break;
}