Pages

Monday, March 28, 2011

Visual Studio C# Express 2010: Your first application!


   To write C# applications you need an integrated development environment (IDE), there is lot of them, like Monodevelop, Charpdevolop, and Visual Studio.
   The best one is Visual Studio, it’s from Microsoft and provides full support for the .NET Framework and Microsoft Platform.
   Visual Studio 2010 is the latest version, it will costs you around 549$ (more information here) , but Microsoft have a free edition for educational purpose, named Visual C# Express which you can download for free along with other Express products, and it’s the one we are going to use.
   Exploring Visual C# Express 2010: Creating a simple application:
    After you finished the download, just follow the steps on the setup screen, it’s an easy step (If you don’t know how to setup an application, so there is no need to learn development!) and we won’t discuss.
   Now, open the Visual C# and let’s create a new Windows Application, click the Create Project then select Console Application, choose a name (MyFirstApp for example) and click OK.
3-27-2011 1-53-56 PM
   As you can see, the IDE has created a file for you: program.cs, There is one thing you have to understand, the C# compiler will look for a method named Main() in a file named program.cs, it’s the main method in your application that you must have in your program, otherwise, you will get an error.
   So let’s start coding a simple “Hello World” application. In the Main() method write: Console.WriteLine(“Hello World!”); 
3-28-2011 2-11-30 PM 
   Let’s execute it and see the result, just click the green arrow or press F5.
   3-28-2011 2-15-29 PM




   Funny no? You just wrote an application!
   Now let’s understand what we did, first we asked a pretty lady named Console for a service that she can do for us, we asked her to write something for us in the screen, so she said “ok,give me the sentence” and we gave her Hello World.
   The pretty lady is a class, shown in sky blue color, we asked her to do us the service by calling an existing method that she exposes called WriteLine,and we told her about our sentence by passing it via parameters
  So, you might ask: “Cool, can I create a pretty lady that can calculate numbers?” sure! do want to see a demo? so let’s create a new class file named: Calculator.cs.
3-28-2011 2-22-42 PM
   Now you have a new file which can contains classes, Visual C# has created for us one class named: MyCalculator,but you can create as much as you want in one single file!
   Ok, until now, our class stands for nothing, so let’s make it useful and add some code; First, declare a method named Add, that accept two parameters and return a variable of type int (integer).
public int Add(int n1, int n2)
        {
            int result = n1 + n2;
            return result;
        }
3-28-2011 2-31-45 PM
  Now, we have our genius lady that can calculate! Let’s ask here for a service!
  Go back to our first file program.cs and add the following code in the Main method:
MyCalculator MyGeniusLady = new MyCalculator();
            int result = MyGeniusLady.Add(5, 6);
            Console.WriteLine("5 + 6 = " + result);
            Console.ReadKey();
3-28-2011 2-36-41 PM
   Let’s execute our program and see the result!
3-28-2011 2-38-09 PM
   Wow! Brilliant no? So what just happened? Let’s explain:
   First, we wrote a method that takes 2 parameters, work with them then return the result as a variable of type Int (return result;). Once the return keyword (In Visual C#, keywords are colored with blue) is reached by the JIT the methods ends, so any thing you write after this word has been reached won’t be executed!
   In our method we used variables which are places in the memory where we can put our data temporally, all our variables will despair once our program exits, so we can use them only to store user input temporally.
   Now, we are going to extend our program so it takes user input and work with it rather than our 5 and 6! We need just to modify the code in Main() so it looks like this one:
static void Main(string[] args)
        {
            MyCalculator MyGeniusLady = new MyCalculator();           
            int result = MyGeniusLady.Add(5, 6);
            Console.Write("Please enter the first number A: ");
            int A = Console.Read();
            Console.Write("Please enter the first number B: ");
            int B = Console.Read();
            Console.WriteLine(MyGeniusLady.Add(A,B));
            Console.ReadKey();
        }
3-28-2011 2-50-54 PM
   Now Test It!
3-28-2011 2-56-18 PM
   What have we done is: we took the user input with the ReadLine() method which returns a String and converted it to Int with the Parse() method and then we assigned it to our variable; You can also see that we wrote this: Console.WriteLine(MyGeniusLady.Add(A,B)); because our method return an Int, we can simply just pass it to the WriteLine method! this has the same result as in the previous example but we just did it in a deferent way!
  Notice: you see the ReadKey() method at the end of the method, we just you it to prevent the console from automatically closing after showing the result. Remove it and test!
   You may ask: “Wait a moment! My lady is not like the Console lady! I had to write more code to ask her to give me some results!” The answer is simple! The console class is static , that means you can call her directly without the need of assigning a variable with a reference to a new instance, Our class is not static, we can create as many variables as we want! But you can always make you class static by adding the static keyword after the public keyword. (Static classes will be discussed in another lesson!)
   This is all for today, Try to understand it! If you need more information about Classes, Console, Methods… etc. please refer to the MSDN Web Site.

No comments:

Post a Comment