Post: C# Programming Basics Tutorial
10-03-2014, 03:25 PM #1
Default Avatar
Bch
Guest
(adsbygoogle = window.adsbygoogle || []).push({}); I spent a fair few hours on this tutorial. If you have any questions just ask below Smile


Structure

    using System;
namespace tutorial1App
{
class Form1
{
int Number = 0;

private void Form1_Load(object sender, System.EventArgs e)
{
// Comment type 1

/* Comment type 2
Part of comment type 2
This line is still a comment*/

MessageBox.Show("Hello world");
}

private void ErrorMessage(string Text)
{
MessageBox.Show(Text);
}
}
}


Line 1 - Using System;
This allows the program to use the System; a program will generally have multiple "Using" statements.

Line 2 - A namespace declaration is a collection of classes.

Line 3 - An Curly Open bracket, resembles the start of that function/procedure, the closed curly bracket resembles the end. So all the code within that function will be inside the { and }.

Line 4 - In this case the class is automatically generated as a Form class.

Line 6 - A Variable is a reserved space in memory. It can hold memory, of different types that can change. Some types of varaibles you may use are; Int, Bool, Float, Decimal, String. This line creates a variable and gives it a name of Number. The type of variable is an "int" which means integer, also known as a Whole Number, and is given the value of 0.

Line 8 - A private void is a procedure which can be called. In this case it is called when an event is taken place. The "On Load" event, which means when the program loads up, it will run the code within this procedure.

Line 10 - This line is a comment, which means the program will ignore this line. The "//" makes this single line a comment

Line 12 - This is a multi line comment, the start of the comment is /* and everything after that is classed as a comment, until you type the end */

Line 14 - MessageBox.Show is a procedure that is built into visual studios. Calling this function will show a message box. The string (text) inside the brackets, needs to have a quotation mark at either end. This is classed as one argument. MessageBox.Show can support multiple arguments, such as Icons and different button options.

Line 17 - This is a procedure that you would call by doing ErrorMessage("This is text for the error message");, this is simalir to OnLoad, however does not have an event attached to it, which means it will only run this procedure when you call it. It only can handle one argument, which in this case is a variable named "Text" and the type of variable is a string. (string Text)


Data Types

Bool / Boolean - A bool is the easiest data type. As it only has two values, either True or False.
String - A String is simply text, it can contain numbers and letters. However if you was to add "2" + "6" as a string, the output would be "26". As it simply joins them together.
Int - An integer holds a number, from -2,147,483,648 to 2,147,483,647(Int 32). If the range needs to be larger, you would use a long. (Int 64)
Byte - A Value from 0 - 255 as an integer, or 0x00 to 0xFF as a hexidecemal.
Char - A single character, such a "a" or "&".
Uint - 32 Bit unsigned integer type, with a range of 0 - 4,294,967,295.
Float - 32 Bit single precision floating point type. An example would be "36.512f"
Pointer - A pointer type variable stores the memory address of another type.


Conversion

Use the Convert.To (Variable type) Method to convert one variable type to another.
An Example would be Convert.ToInt32("1754" + "1253") + 5; This line would first add the two strings "1754" + "1253" to make 17541253 then Convert it to a number. Then add 5 therefore equaling 17541258
Other exampels are Convert.ToString(); Convert.ToByte(); and so on


Operators

+ Add two operands
- Subtract two operands
* Multiply two operands
/ Divide two operands (Will get error if your result data type is Int, and it results in a decimal number)
++ Increment operator. So 5++ will = 6
-- Decrement operator. So 13-- Will = 12


Relational Operators

== Checks if two values equal each other.
!= Checks if two values are not equal.
> Checks if the left value is greater than the right value.
< Checks if the left value is less than the right value.
>= Checks if the left value is greater than or equal to the right value.
<= Checks if the left value is less than or equal to the right value.


Logical Operators

&& Checks if one operator AND the next operator is true. E.g. (A == true && B == true) If both equal true
|| Checks if one or the other is true.
! Checks if it is the opposite to the statement. Eg If !(A == true && B == true) So if they both Do NOT equal true


Statements

If statement

And if statement will check if something is correct, if it is then it will run the code between { }
Example
    
bool example = true;
if (example == true)
{
MessageBox.Show("Example is true!");
}



If else statements

If the statement is correct it will run the code, however if it isn't correct, it will run the else code
Example
    
bool example = true;
if (example == true)
{
MessageBox.Show("Example is true!");
}
else
{
MessageBox.Show("Example is false!");
}



Switch Statements

A switch statment is another way of doing lots of if statements, that checks the value of a variable
Example
    
int number = 3;
switch(number)
{
case 0:
MessageBox.Show("Number = 0");
break;
case 1;
MessageBox.Show("Number = 1");
break;
case 2;
MessageBox.Show("Number = 2");
break;
case 3;
MessageBox.Show("Number = 3");
break;
case 4;
MessageBox.Show("Number = 4");
break;
}

In this case, the message will say Number = 3


For statement

This will repeat a section of code, until a value == another value
Example
    
for (int i = 0; i =< 8; i++)
{
MessageBox.Show("i =" + Convert.ToString(i));
}


The output will be 9 Messageboxes, each with a number, incrementing from 0 - 8

While Loop
A while loop will loop something, until the operator is not true
Example
    
int i = 0;

While (i < 5)
{
MessageBox.Show("i =" + Convert.ToString(i));
i++
}

This will output 5 Messageboxes, from 0 - 4


Do While Statements

Similiar to a while statement, however this will run the code atleast once, before it checks if it is true, where as the while statement checks it before the code is run.
Example
    
do
{
MessageBox.Show("i =" + Convert.ToString(i));
i++
} while (i < 5);




Methods/Procedures

A method/procedure, is where you can call the something multiple times. You can input variables, and it can also return variables.
Pythagoras Example // If you don't know, pythagorean theorem - a² + b² = c²
    
public static double Pythagoras(int A, int B)
{
int sqaure = (A*A) + (B*B);
return Math.Sqrt(square);
}


In this case the i have used the variable "double" to be returned. As the result may be a decimal. However if you do not want the method to return a value, use "void".
The "(int A, int B)" are the arguments that are required to call the function. If you do not want to call any arguments, you will just do ().

An example in code to use this method would be as followed
    
void Button1_Click(Object sender, EventArgs e)
{
MessageBox.Show("Side A = 5, Side B = 5, Side C = " + Convert.ToString(Pythagoras(5, 5));
}


This will show a messagebox saying "Side A = 5, Side B = 5, Side C = 7.07106"


Arrays

An array is used to store lots of variables of the same type.
An example of an array, would be a list of names, as you can have many names, but they will all be a string.

Syntax Example
    string[] PersonName = new PersonName[50];

The above line will create a string array named PersonName, which has 50 string slots.
Then you could enter the values within the array by doing
    PersonName[0] = "Tom";
PersonName[1] = "Peter"

...

Assigning values from an array can be done while declaration, by doing
    string[] PersonName = { "Tom", "Peter", "James" };



Events

An event runs a section of code, once the event has been done. A simple well known example would be the event On Click, which will run a section of code when you click the item.
There are other events such as "On Text Changed" which will run the code every time a new character has been entered.


Timers

A timer is an object in the tool box, one of the most important ones i use.
The timer has a propertie which sets the Time it takes until it next "Ticks".
The OnTick event will run the code every time the timer "ticks".


So that is the very basics of C# programming, it is also good to know that it can be ported to many other programming languages. Although the syntax is different, it will not take you too long to learn it. For example i first learn Pascal Lazarus, but then learnt C#, and alot of information i have transfered across while learning new things.
I recommend you make simple, basic programs to learn the GUI design, how things work, and use the above as a reference through your learning. I have made over 100 random small programs, to learn key skills, i've made simple encryption programs, data manipulation programs and i still do just to make things easier. For example, if there is a textfile, with over 1000 lines of text, but i want the text to delete a part, and also make it all capital letters, it would take me hours and hours to manually do, therefore i created a program and after 10minuites of programming, and two seconds of run time, the file was changed.

PS3 Related

PS3Lib Related - So this is what you all want, right? :P
First of all, you will need to add the PS3Lib; dll as a reference in your program. To do this click Project, then Add Reference, then browse for the dll.
Now that you have added it as a reference, you will need to add "Using PS3Lib;" This will allow to use the resources within PS3Lib to allow you to connect your program to your PS3.

You will need to add an instance, within your program where you declare your variables. "PS3API PS3 = new PS3API();" //PS3API() Takes 1 optional Argument
Now, add a button called Connect, then add the event "On Click" to your code, by clicking the lightning tab (events) then double clicking the On Click event.
Next, view all the options you can do within PS3Lib by type PS3. , this will show you all the options, use your UP and Down arrows to scroll. Some options, have more options within them such as PS3.Extension.
So for the Conenct button, you want to add PS3.Connect(), this method returns a bool value, and has an optionary argument for an IP address which is a string E.g. "192.168.1.1".
To check if it has connected or not, you should add this to an IF statement. Use the above If Statment as a template. Use the same method for Attaching to your PS3.

Now to edit the ps3 games memory in real time, you will need to know the Offset of the game. If the offset is dynamic, you will need to make a search function.
This is one simple example of how to change your name for MW2, the Name offset for MW2 is 0x1F9F11C.
Example
    PS3.Extension.WriteString(0x1F9F11C, "Test");


This will change your name to Test, to read the current name and display it in a messagebox, you would do the following.
    MessageBox.Show(PS3.Extension.ReadString(0x1F9F11C));



If people have any questions, or suggestions on any more detail, be sure to comment below and i will be happy to help. This tutorial took me a few hours to write up, i add picture if people would prefer. I decided not to add GUI set up information as most of it, is self explanatory.

Basic MW2 RTM Tool Source Created For This Tutorial By Me and Its Welsh. - You must login or register to view this content.
Commented most lines explaining what they do.
You must login or register to view this content.



- Son Of A Beach
Last edited by Bch ; 10-03-2014 at 06:23 PM.

The following 35 users say thank you to Bch for this useful post:

One, A Friend, Ariel R., AZ-Gun-Man-1, bAdReQuEsT, byVexHD, Cyb3r, dactylus, Father Luckeyy, gstar555, gtarag, hibye3, Hori_By_Nature, Welsh, jagex, Kaffeeklatsch, klambo, lahyene77, Mauzey, MICKEY_MODZ, milhous, Norway-_-1999, Pichu, RC_Dub1, RTE, ScaRzModZ, Smoky420, Snoop, Terrorize 420, Winter, witchery, xXx-M83-xXx, zKillShxt
10-10-2014, 11:39 PM #20
Modz-killer1
Gym leader
Originally posted by Beach View Post
Probably, but why don't you? The informations there, anything that's not there I'm sure you can You must login or register to view this content. it


Idk m8 im not a Programmer i just use modz it will be great when you could make a non host tool for bf4 Smile nobody release a tool for it ;S
10-12-2014, 08:15 AM #21
Originally posted by Beach View Post
I spent a fair few hours on this tutorial. If you have any questions just ask below Smile


Structure

    using System;
namespace tutorial1App
{
class Form1
{
int Number = 0;

private void Form1_Load(object sender, System.EventArgs e)
{
// Comment type 1

/* Comment type 2
Part of comment type 2
This line is still a comment*/

MessageBox.Show("Hello world");
}

private void ErrorMessage(string Text)
{
MessageBox.Show(Text);
}
}
}


Line 1 - Using System;
This allows the program to use the System; a program will generally have multiple "Using" statements.

Line 2 - A namespace declaration is a collection of classes.

Line 3 - An Curly Open bracket, resembles the start of that function/procedure, the closed curly bracket resembles the end. So all the code within that function will be inside the { and }.

Line 4 - In this case the class is automatically generated as a Form class.

Line 6 - A Variable is a reserved space in memory. It can hold memory, of different types that can change. Some types of varaibles you may use are; Int, Bool, Float, Decimal, String. This line creates a variable and gives it a name of Number. The type of variable is an "int" which means integer, also known as a Whole Number, and is given the value of 0.

Line 8 - A private void is a procedure which can be called. In this case it is called when an event is taken place. The "On Load" event, which means when the program loads up, it will run the code within this procedure.

Line 10 - This line is a comment, which means the program will ignore this line. The "//" makes this single line a comment

Line 12 - This is a multi line comment, the start of the comment is /* and everything after that is classed as a comment, until you type the end */

Line 14 - MessageBox.Show is a procedure that is built into visual studios. Calling this function will show a message box. The string (text) inside the brackets, needs to have a quotation mark at either end. This is classed as one argument. MessageBox.Show can support multiple arguments, such as Icons and different button options.

Line 17 - This is a procedure that you would call by doing ErrorMessage("This is text for the error message");, this is simalir to OnLoad, however does not have an event attached to it, which means it will only run this procedure when you call it. It only can handle one argument, which in this case is a variable named "Text" and the type of variable is a string. (string Text)


Data Types

Bool / Boolean - A bool is the easiest data type. As it only has two values, either True or False.
String - A String is simply text, it can contain numbers and letters. However if you was to add "2" + "6" as a string, the output would be "26". As it simply joins them together.
Int - An integer holds a number, from -2,147,483,648 to 2,147,483,647(Int 32). If the range needs to be larger, you would use a long. (Int 64)
Byte - A Value from 0 - 255 as an integer, or 0x00 to 0xFF as a hexidecemal.
Char - A single character, such a "a" or "&".
Uint - 32 Bit unsigned integer type, with a range of 0 - 4,294,967,295.
Float - 32 Bit single precision floating point type. An example would be "36.512f"
Pointer - A pointer type variable stores the memory address of another type.


Conversion

Use the Convert.To (Variable type) Method to convert one variable type to another.
An Example would be Convert.ToInt32("1754" + "1253") + 5; This line would first add the two strings "1754" + "1253" to make 17541253 then Convert it to a number. Then add 5 therefore equaling 17541258
Other exampels are Convert.ToString(); Convert.ToByte(); and so on


Operators

+ Add two operands
- Subtract two operands
* Multiply two operands
/ Divide two operands (Will get error if your result data type is Int, and it results in a decimal number)
++ Increment operator. So 5++ will = 6
-- Decrement operator. So 13-- Will = 12


Relational Operators

== Checks if two values equal each other.
!= Checks if two values are not equal.
> Checks if the left value is greater than the right value.
< Checks if the left value is less than the right value.
>= Checks if the left value is greater than or equal to the right value.
<= Checks if the left value is less than or equal to the right value.


Logical Operators

&& Checks if one operator AND the next operator is true. E.g. (A == true && B == true) If both equal true
|| Checks if one or the other is true.
! Checks if it is the opposite to the statement. Eg If !(A == true && B == true) So if they both Do NOT equal true


Statements

If statement

And if statement will check if something is correct, if it is then it will run the code between { }
Example
    
bool example = true;
if (example == true)
{
MessageBox.Show("Example is true!");
}



If else statements

If the statement is correct it will run the code, however if it isn't correct, it will run the else code
Example
    
bool example = true;
if (example == true)
{
MessageBox.Show("Example is true!");
}
else
{
MessageBox.Show("Example is false!");
}



Switch Statements

A switch statment is another way of doing lots of if statements, that checks the value of a variable
Example
    
int number = 3;
switch(number)
{
case 0:
MessageBox.Show("Number = 0");
break;
case 1;
MessageBox.Show("Number = 1");
break;
case 2;
MessageBox.Show("Number = 2");
break;
case 3;
MessageBox.Show("Number = 3");
break;
case 4;
MessageBox.Show("Number = 4");
break;
}

In this case, the message will say Number = 3


For statement

This will repeat a section of code, until a value == another value
Example
    
for (int i = 0; i =< 8; i++)
{
MessageBox.Show("i =" + Convert.ToString(i));
}


The output will be 9 Messageboxes, each with a number, incrementing from 0 - 8

While Loop
A while loop will loop something, until the operator is not true
Example
    
int i = 0;

While (i < 5)
{
MessageBox.Show("i =" + Convert.ToString(i));
i++
}

This will output 5 Messageboxes, from 0 - 4


Do While Statements

Similiar to a while statement, however this will run the code atleast once, before it checks if it is true, where as the while statement checks it before the code is run.
Example
    
do
{
MessageBox.Show("i =" + Convert.ToString(i));
i++
} while (i < 5);




Methods/Procedures

A method/procedure, is where you can call the something multiple times. You can input variables, and it can also return variables.
Pythagoras Example // If you don't know, pythagorean theorem - a² + b² = c²
    
public static double Pythagoras(int A, int B)
{
int sqaure = (A*A) + (B*B);
return Math.Sqrt(square);
}


In this case the i have used the variable "double" to be returned. As the result may be a decimal. However if you do not want the method to return a value, use "void".
The "(int A, int B)" are the arguments that are required to call the function. If you do not want to call any arguments, you will just do ().

An example in code to use this method would be as followed
    
void Button1_Click(Object sender, EventArgs e)
{
MessageBox.Show("Side A = 5, Side B = 5, Side C = " + Convert.ToString(Pythagoras(5, 5));
}


This will show a messagebox saying "Side A = 5, Side B = 5, Side C = 7.07106"


Arrays

An array is used to store lots of variables of the same type.
An example of an array, would be a list of names, as you can have many names, but they will all be a string.

Syntax Example
    string[] PersonName = new PersonName[50];

The above line will create a string array named PersonName, which has 50 string slots.
Then you could enter the values within the array by doing
    PersonName[0] = "Tom";
PersonName[1] = "Peter"

...

Assigning values from an array can be done while declaration, by doing
    string[] PersonName = { "Tom", "Peter", "James" };



Events

An event runs a section of code, once the event has been done. A simple well known example would be the event On Click, which will run a section of code when you click the item.
There are other events such as "On Text Changed" which will run the code every time a new character has been entered.


Timers

A timer is an object in the tool box, one of the most important ones i use.
The timer has a propertie which sets the Time it takes until it next "Ticks".
The OnTick event will run the code every time the timer "ticks".


So that is the very basics of C# programming, it is also good to know that it can be ported to many other programming languages. Although the syntax is different, it will not take you too long to learn it. For example i first learn Pascal Lazarus, but then learnt C#, and alot of information i have transfered across while learning new things.
I recommend you make simple, basic programs to learn the GUI design, how things work, and use the above as a reference through your learning. I have made over 100 random small programs, to learn key skills, i've made simple encryption programs, data manipulation programs and i still do just to make things easier. For example, if there is a textfile, with over 1000 lines of text, but i want the text to delete a part, and also make it all capital letters, it would take me hours and hours to manually do, therefore i created a program and after 10minuites of programming, and two seconds of run time, the file was changed.

PS3 Related

PS3Lib Related - So this is what you all want, right? :P
First of all, you will need to add the PS3Lib; dll as a reference in your program. To do this click Project, then Add Reference, then browse for the dll.
Now that you have added it as a reference, you will need to add "Using PS3Lib;" This will allow to use the resources within PS3Lib to allow you to connect your program to your PS3.

You will need to add an instance, within your program where you declare your variables. "PS3API PS3 = new PS3API();" //PS3API() Takes 1 optional Argument
Now, add a button called Connect, then add the event "On Click" to your code, by clicking the lightning tab (events) then double clicking the On Click event.
Next, view all the options you can do within PS3Lib by type PS3. , this will show you all the options, use your UP and Down arrows to scroll. Some options, have more options within them such as PS3.Extension.
So for the Conenct button, you want to add PS3.Connect(), this method returns a bool value, and has an optionary argument for an IP address which is a string E.g. "192.168.1.1".
To check if it has connected or not, you should add this to an IF statement. Use the above If Statment as a template. Use the same method for Attaching to your PS3.

Now to edit the ps3 games memory in real time, you will need to know the Offset of the game. If the offset is dynamic, you will need to make a search function.
This is one simple example of how to change your name for MW2, the Name offset for MW2 is 0x1F9F11C.
Example
    PS3.Extension.WriteString(0x1F9F11C, "Test");


This will change your name to Test, to read the current name and display it in a messagebox, you would do the following.
    MessageBox.Show(PS3.Extension.ReadString(0x1F9F11C));



If people have any questions, or suggestions on any more detail, be sure to comment below and i will be happy to help. This tutorial took me a few hours to write up, i add picture if people would prefer. I decided not to add GUI set up information as most of it, is self explanatory.

Basic MW2 RTM Tool Source Created For This Tutorial By Me and Its Welsh. - You must login or register to view this content.
Commented most lines explaining what they do.
You must login or register to view this content.



- Son Of A Beach


Looks way similar to Java. And I wasn't very good at that, lol. And supposedly C#/C++ are more difficult than Java. I'm more a web designer, since HTML and CSS are easy for me. But I'll look through this guide anyway.
10-12-2014, 08:39 AM #22
Default Avatar
Bch
Guest
Originally posted by Zombie
Looks way similar to Java. And I wasn't very good at that, lol. And supposedly C#/C++ are more difficult than Java. I'm more a web designer, since HTML and CSS are easy for me. But I'll look through this guide anyway.


To be honest, most programming languages are very very similar, Java is meant to be the most different programming language, and for CSS well thats 4th Generation, C# is only 3rd generation programming :p . If you need any help just add me on skype :p "beachmodding"
10-16-2014, 11:06 AM #23
can you put a link to download the program you use
10-16-2014, 04:46 PM #24
Default Avatar
Oneup
Guest
Originally posted by jaymzie View Post
can you put a link to download the program you use

If you are asking about what he programs in, it's more than likely visual studio which you can get for free from microsoft
You must login or register to view this content.

The following user thanked Oneup for this useful post:

10-18-2014, 03:03 AM #25
Some people just can't do it. I've read it, and it's another language to me. I'd end up punching holes in my walls.
Originally posted by Beach View Post
Probably, but why don't you? The informations there, anything that's not there I'm sure you can You must login or register to view this content. it
10-18-2014, 06:53 AM #26
Default Avatar
Bch
Guest
Originally posted by emski022 View Post
Some people just can't do it. I've read it, and it's another language to me. I'd end up punching holes in my walls.


Of course it's hard to learn at first, this tutorial is just giving you some idea on all the logic and syntax, you will also want to watch youtube videos, look at peoples sources they have released, and the main thing is practise writing program's!
10-18-2014, 08:00 PM #27
I'm a graphics designer for 8+ years. I have tried coding in the past, and my brain just can't handle it. I lose my temper. lol It might seem like people don't appreciate you guys releasing cool stuff, but we really do, but there's always those fools that leave hate and ruin it for others. Anyway. Thanks for the chat. Cheers
Originally posted by Beach View Post
Of course it's hard to learn at first, this tutorial is just giving you some idea on all the logic and syntax, you will also want to watch youtube videos, look at peoples sources they have released, and the main thing is practise writing program's!
10-24-2014, 03:25 AM #28
jagex
Gym leader
Nice job, maybe you can add nested statements as well?
Last edited by jagex ; 10-24-2014 at 03:27 AM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo