Programming Tutorials: Basics

Post Reply
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Programming Tutorials: Basics

Post by OwnZ joO »

Ok so to start learning programming, you are going to need to know how to declare a variable.
In order to declare a variable in C#, first you put the Type of variable, and then the variable name.

Code: Select all

Type variableName;
There are many types of variables that you can declare, but some more common ones that you will use, especially in beginning to program include the number data types.
Obviously I'm not going to list them all off, if you continue learning to program, then you will learn them when you need to.

int - a whole number(an integer, no fractions allowed)
float - a real number(can have a decimal point)
byte
short
long

The only difference that is important in the byte short int and long data types to us is the size of the numbers that they can hold.

So in order to declare an integer you need to make this statement

Code: Select all

int myIntegerVariable;
In order to change the value in the variable you need to use the assignment operator =
The .Net framework will automatically instantiate variables to their default values, for numbers it is 0
so in the above code myIntegerVariable would have a value of 0 even though I didn't assign 0 to it

So if I want to instantiate my integer variable to 45 there are multiple ways to do this that achieve the same result

Code: Select all

int num;
num = 45;
or
int num = 45;
Either of those statements work to create a variable holding an integer value of 45, most of the time you will assign a value to number variables when you create them.

Some other examples of creating variables

Code: Select all

bool myBooleanVariable = false;
float myFloatVariable = 3.14;
int myIntVariable = 420;
Now that hopefully you have learned to declare variables, we can move onto methods.

Methods are where the work gets done in programs. Methods are functions that either return a Data Type or void(no return value)

So to declare a function you first declare the return Type, then the name

Code: Select all

ReturnType MethodName()
{
...
}
Say for example I wanted to create a method that returns a bool(true or false) it would look something like this

Code: Select all

bool IAmLearningToProgram()
{
   return true;
}
Well that's nice and all but it doesn't do us much good at this point to declare a function with a return value that was already known.
To make a useful method at this point we will need to add what are called parameters
Parameters are values that are passed into the method
You declare parameters in the method declaration, they are like variables that can be accessed inside of your method.
For example say we wanted to have a method that takes two integers as parameters and adds them together and doubles them and returns that you could do it this way

Code: Select all

int DoubleSum(int firstNum, int secondNum)
{
  int sum = firstNum + secondNum;
  return sum * 2;
}
Notice how I declared a variable to store the sum of the two parameters. I didn't necessarily have to do this, I could have done all of the calculations in one line, but it's a good example with variables. One thing to note about the variable sum is that it can't be accessed outside of the DoubleSum method, it is declared inside of it, so it can only be accessed inside of it.

Now declaring a method doesn't do much good unless you know how to call this method. I am going to assume that you created a Command Line project in Visual Studio. So you should have something like this:

Code: Select all

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
You will want to declare your method inside of the class Program
Well when your program runs, it will automatically call the Main method, so from there you can call other methods if you wish.
Here is an example of a program that will ask the user for two numbers, add them together, and double that, then it will display the result

I will be using some methods from the Console class that will allow me to read and write to the Command Line display.
Console.ReadLine() will return a string when the user types into the command line and then presses enter
Console.WriteLine("....") will write a string to the Command Line
int.Parse("...") takes in a string and returns an integer
Console.ReadKey() just reads the next character the user enters, so it will wait until the user presses another key and then finish with the Main method, which ends the program

Code: Select all

    class Program
    {
        static void Main(string[] args)
        {
          Console.WriteLine("Please enter two numbers");
          int num1 = int.Parse(Console.ReadLine());
          int num2 = int.Parse(Console.ReadLine());
          int summedAndDoubled = DoubleSum(num1, num2);
          Console.WriteLine(num1 + " and " + num2 + " added together and doubled = " + summedAndDoubled);
          Console.WriteLine("Press any key to exit...");
          Console.ReadKey();
        }

        static int DoubleSum(int firstNum, int secondNum)
        {
          int sum = firstNum + secondNum;
          return sum * 2;
        }
    }
Go ahead and make your Project look like that, and press the green arrow or F5 to run the program.

Some of you probably noticed that the method DoubleSum now has static in front of it. Don't pay attention to that right now, just know that the methods you define inside of the class Program need to be static in order to call them. So basically what happened in our main function is we read two numbers from the Command Line, passed those numbers as parameters into our DoubleSum method and stored the return value in the variable summedAndDoubled.

Hopefully you learned a little bit about some basic programming. I am not the best at organizing my thoughts sometimes, so if you need something explained better or are confused, don't be afraid to ask. Hopefully this was a decent tutorial, if not I'm sure I will end up rewriting it to make it more understandable in the future.
Attachments
BasicProgramExample.7z
Basic Program Example Project
(12.85 KiB) Downloaded 377 times
User avatar
Click16
Posts: 1941
Joined: Mon Dec 31, 2007 4:36 am
Location: United States

Re: Programming Tutorials: Basics

Post by Click16 »

Even though i can't code in CSharp too well, It looks like you put a good amount of work into this. Nice job!

First post *woot*
User avatar
XZodia
Staff
Posts: 2208
Joined: Sun Dec 09, 2007 2:09 pm
Location: UK
Contact:

Re: Programming Tutorials: Basics

Post by XZodia »

I have to say I was expecting myself to criticize this so much, but after reading through it I found it to be very comprehensive.
Image
JacksonCougar wrote:I find you usually have great ideas.
JacksonCougar wrote:Ah fuck. Why must you always be right? Why.
User avatar
OwnZ joO
Posts: 1197
Joined: Sun Dec 09, 2007 4:46 pm

Re: Programming Tutorials: Basics

Post by OwnZ joO »

Thanks, it's a lot tougher than I thought it would be to write this. I obviously know all the basic stuff, but trying to write it out in a way that's simple and covers what needs to be covered was more difficult than I planned.
Post Reply