C# Programming: 2012

Saturday, October 6, 2012

C# Hello World Programme

Install Visual Studio if not installed already . Visual Studio is an IDE (Integrated Development Environment) in which we can develop our applications using the languages like C++,C#,VB etc or even Java & much more other languages.
    Ok let's begin our simple C# project named "Hello world". " Hello world"  is usually the first program a newbie or newcomer learn when he/she was new to any platform or language.Here platform may be IDE's or Operating Systems(OS) and languages may be c,C++,C#,Java,Python etc.
       To begin a new project.Follow the following steps:
1.Open Visual Studio
2.Click on  File
3.Click on New Project
4.Select language C#
5.Select template or project type-Console Application
6.Give a name to your Project.eg-HelloWorld
        As default a code editor will open containing some default codes which is provide by IDE itself for our comfort.
   Believe me programming in Microsoft Visual Studio is very easy due to this default codes (no need to type same codes for every application) & ' Intellisense ', for example , if you are attempting to write a code "Console.WriteLine()".After typing "console." & "w" .WriteLine will appear itself & you can press enter or other letters after WriteLine to use that word.
        Let's code our "Hello World" Programme:-

      //I am a comment you must know ,i will describe working of any code..
//------Code Start-------------------------
 using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
Console.WriteLine("Hello World!");
Console.ReadLine();
        }
    }
}
//---------------Code End--------------------------
   In your IDE you code will look like this
Let's understand each and every part of above code in detail.
Using keyword is used to include any directives or header files or namespaces.Directives are required by our applications to use  any function.
       for eg- Console.WriteLine();
WriteLine() is a function.Console is a class.System is a namespace.System is a collection of classes like console.So we can say namespace is a collection of similar classes.System namespace must be there in every console I/O operation.

Previously we discuss about the namespace 'System' . So what's difference in between System and HelloWorld namespace? So here is a difference,System is defined by IDE & HelloWorld is defined by ourself .Suppose you are doing system programming i.e. developing Operating System(OS) yourself ;).At that time you could not use any directives or namespace.You have to define yourself each and every work or functions.Since we are doing application programming ,we require some pre-built functions thats why we use namespace like System.So why there are these namespaces,classes,functions, etc.These all are parts of OOP(Object Oriented Programming).Class contain functions and data.Whereas namespace contains similar classes i.e namespace is a grouping of similar classes.

This curly starts the definition of  HelloWorld namespace.

This is a class which contains some function or any data.Here we are using only function .eg-'Main()' function.'Program' is optional you can give any name like in namespace naming.

This curly starts 'Program' class definition.

Here, 'Main' function is a starting entry point of application.Like your first line of notebook.Since C sharp(C#) is a case sensitive programming language,So M must be capital in Main.Oh! i forgot to tell our 'System' namespace's  'S' is also Capital ;)
  Here Void means our function does not return anything.That is nothing in response to that point
 from which our function is called.Since Main() will not be called from any other part of our program.Since it is a entry point of our application.

So now 'Static', static functions are those functions which does not require any instance identifier.They are called with the type name.By reading this please don't get into complexity on this very first program.Whole chapter may create dealing with static.You only have to know at this time is that
this "static Void Main()" line is a entry point of our application and each application must have a Main() function.



This Curly brace stats our function definition.



Console is a class under System namespace .WriteLine() is a function defined in console  class.
Dot or period is used to access the function of any class.Console means DOS like screen.To print
any message on that screen or window we use Console.WriteLine() function.Hence WriteLine function is used to write a Line in screen.If you only use Console.Writr() instead of WriteLine(), a line will not be breaked.
The message or string must be inside double quotations.Since it is a string(eg "Iphone") ie combination of 'characters(eg 'I') .Belive me nohing is basic before this :( .Braces are of WriteLine function.There will not be any function without braces,so if there is anything inside braces then that is a parameter or arguments on which the function depends or works.





ReadLine() is actually for reading a line typed by user.But here we use
it for holding the screen for a while to see the output untill user presses a enter key.Then why to hold screen?(Ans:Try skipping this line you will notice ;) ).oh yea another basics goes here for semicolon(;).Semicolon at last indicates that one statement or command for the computer is finished haha :)
   each namespace ,class & function must start & end with matching curly braces.
     So,this is a end of our hello world program. i hope you guys learn a basics in very easy & rocking way.See you in next post ,ba-bye tc :)







Friday, October 5, 2012

Simple OOP IN C#

Welcome to my 2nd post on C# programming with a title "Simple OOP IN C#" .So lets begin our project demonstrating a simple OOP(Object Oriented Programming or Paradigm) in C# language.
  Start a new console project in Microsoft Visual Studio or C# express .
File>>New Project>>Visual C#>>Console Application>> Name it "OOP"
 When you finish giving name to the project as default a code editor as below will displayed, as our application will not contain any graphics so only console (ie white text on black screen usually ;) )
So Nothing to design here lets begin to code:

//code of OOP.cpp starts here...
using System;  
 // these are directives ..Notice the S in System its always capital ;) 
 //since c# is a case sensitive language

namespace OOP         //namespace is group of similar classes
{

    class Man           //contains properties or data and functions usually, object of class is used to access them :)
    {
        //properties
        public string name;//if we dont use public it wont be accessible to other classes
        public string address;

        //function
        public void show_info()  //this function will show information
        {
            Console.WriteLine("{0} is from {1}", name, address);

        }
    }
 // thus Man class finished with properties and a function ,can you believe how siple our task gonna be :)..so dont worry lets go ahead.

    //Now we access Man class by creating  object of Man class
    // object usaully is a way to access data( or properties) and functions of any class,thats very simple.
    //still confused on object ..here in our app name and address are data and show_info is a funtion to show those data in screen.Now its clear ya.
    //But why to use object why not directly...its something like taking appointment for meeting the Boss..something legal procedure...

   
    class Program
    {  
        static void Main(string[] args) //static method does not require object to be created  
        {
            //dont bother with 'static' here we will learn about it on other post ..u just need to know is
            //that this Main function is our application's entry point.

            Man obj1 = new Man(); // object of Man class is created here ,object name is obj1
            //while creating new object ,object type must end with braces i.e Man() <--this braces.
            obj1.name = "Yuvaraj ";
            obj1.address = "Butwal";
            Man obj2 = new Man();
            obj2.name = "Alisa";
            obj2.address = "Lumbini";
            obj1.show_info();
            obj2.show_info();
            Console.ReadLine();

 //used for reading line but here used just to hold the screen untill we see the result.
        }
    }
}

//ok now end the functions,classes,namespace with matching curlys.
----------------------------------------------------------------------------------------------------------
click on green play button to see output:
Now we came to end of our project , i hope you guys enjoyed oop programming :)..bye-bye dear..
Note: Working Codes are on blue color  and comments are on green color.


Wednesday, October 3, 2012

Time Display

Welcome to my very first blog on programming on C#.Net. Today we are going to create a Time Display in c#.
Requrements: Visual studio installed.
steps:
1.Goto File >> New Project >> Select Language (C#) >>Select Template: Windows Form Application.
(This steps will not be in other projects ok guys :)
2.As default a form will appear.

 
Now change the properties of form1 as follows:
 (Right click on form1 and select properties)
  i) Text: Yuvi Timer
  ii) FormBorderStyle: None
  iii)ShowInTaskBar : False
 iv)StartPosition: Manual
 v) Size:403,97
 vi)Location: 875,700 (add according to your resolution)
 vii)BackgroundImage: (Add a image of size  about 403,97)

3.Now add  two components labe1 and a linklabel1 from the toolbox which usually is at left of the IDE(Visual studio). Change the text properties of label1 and linklabel1 as 'welcome' and 'Exit' as shown in picture below.
 Now your form should look like this:

4.Now add another component or object called Timer1 from the toolbox.Add the following properties to timer1:
 i)Enabled=True
ii)Interval=1000   (Here 1000 for 1 sec..ie 500 for 0.5 secs)
5.Now its time for coding :)
 Double Click on form11 and add the following code in form1 class as shown in picture below:
                DateTime current_time= new DateTime() ;    
  
   Double Click on timer11 and add the following code in timer1_tick function as shown in picture below:                   
                    current_time = DateTime.Now;    //to store current time in variable current_time
                    label1.Text = current_time.ToString();          

 //to display the time in label1 ..here text is property of label1 and ToString() is a function of object   //current_time to convert the object to string type.
  Now doubleclick on linklabel1 and inside linklabel1_click function add following code to exit the application:
    Application.Exit();
Now press F5 or press start debugging (green play) buttton to run the application.Now your time display application is ready.To get the exe file goto project folder>>bin>>debug>> and get your_app_name.exe file .Ok Enjoy guys, see you in next post :)