Wednesday, 20 March 2013

HTML(Hypertext Markup Language)

HTML is a presentation language use to present web pages in the browser.HTML is made up of predefined elements and each elements has opening and closing tags and both opening and closing tags must be enclosed in angle brackets.Closing tags must have  the prefix slash(/). HTML is not case sensitive but recommended to write in lower case.

Basic elements of HTML:
  • <html></html>
HTML page must start with opening tag of  <html> and must end with closing tag of </html>
  • <head></head>
This element is use to provide a title for the page, write information related to the page like author of the page. Keywords based on which search engine can find your page and whether or not to refreash the page at specified intervals.
  • <title></title>
This element is used to provide a title for the document which will be displayed within the title bar of the browser. This elements must be inside the heads element.
  • <body></body>
This element is used to specify the actual content to display on the page.
  • Formatting Elements
<h1></h1>
<h2></h2>
<h3></h3> 
<h4></h4>  
<h5></h5> 
<h6></h6> 

This elements are use to create headings from level 1 to the level 6 where level 1 font will be large and from there font size will decrease as the level increase.
  • <p></p>
This element is used to create a paragraph text.
  • <b></b>
Use to make text as bold.
  • <i></i>
Use to make text as italic.

Monday, 18 March 2013

Thread locking or Thread Synchronization

In the below Program all the three threads that are created in program are calling the same method for execution at the same time. So in this case if we execute the program and watch the output we will result that are unexpected.
The problem behind the execution is write now the method is a asynchronous method which can be called by multiple threads at a time.
To overcome the problem with asynchronous we need to first make the method as a synchronous method by adopting the process of thread locking or thread synchronous which can be done putting the methods under the special 'lock' block.
Uncomment the commented section and watch the output.

using System;
using System.Threading;


namespace CSharpConsole
{
    class ThreadDemo4
    {
        Thread t1, t2, t3;
        public ThreadDemo4()
        {
            t1 = new Thread(Display);
            t2 = new Thread(Display);
            t3 = new Thread(Display);
            t1.Start(); t2.Start(); t3.Start();

        }
        // Asynchronous Method
        public void Display()
        {
            Console.WriteLine("[CSharp is  ");
            Thread.Sleep(5000);
            Console.WriteLine("Object Oriented]");

        }
        /* Synchronous Method
        public void Display()
        {
            lock (this)
            {
                Console.Write("[ CSharp is ");
                Thread.Sleep(5000);
                Console.WriteLine("Object Oriented ]");
            }
        }*/
 
            static void Main()
        {
            ThreadDemo4 obj = new ThreadDemo4();
            obj.t1.Join(); obj.t2.Join(); obj.t3.Join();
            Console.ReadLine();
        }

    }
    
}

Windows Programming

     In development of any application we need a user interface(UI) to communicate with the end users where UI's are of 2 types
  • Character User Interface  (CUI)
  • Graphical User Interface (GUI)
     Traditionally we have only CUI. Ex Dos,UNIX,etc. where these application suffers from 2 criticisms like they are not user friendly because we need to learn the commands first to use them. They donot alow to navigate from one place to another
     To solve these problems in early 90's GUI applications were introduced by Microsoft with its windows OS which has a beautiful feature known as look and feel.
     To develop GUI's Microsoft has provided a language also in 90's only ie Visual Basic. Later when .net was introduced the support for GUI has been given in all .net languages.

Developing GUI's
     To develop a GUI we need some special components known as controls which are provided in .net languages in classes in System.Windows.form namespace. All control's which are present under this namespace were grouped into different categories
  • Common Controls
  • Container Controls
  • Menu and Toolbar Controls
  • Dialog Controls
  • Reporting Controls
  • Printing Controls
  • Data Controls
Whatever the control is, every control has 3 things in common
  • Properties: These are attributes of the control which have their impact on look of the control. Ex: Width, Height, Back-color, Fore-color etc.
  • Methods: These are actions performed by the control.  Ex: Clear(), Focus(), Close() etc.
  • Events: These are time periods which specifies when an action has to be performed. Ex: Click, Load, Keypress, MouseOver etc

Friday, 15 March 2013

Multithreading in C#

  • It is an approach which allows a single program to perform multiple actions simultaneously.  Earlier we had an approach of multitasking where in this case we can execute multiple programs at a given point of time. Operating system is responsible for multitasking where all operating systems are not multitasking Operating system. Windows. MAC, Linux are multitasking Operating systems whereas DOS is a single tasking Operating system. If we want to develop a multithreaded application first the support should be available under the programming language whereas the language will internally take the support of Operating system again. C# language supports multitasking.
  • A thread is a unit of execution which is responsible in executing the program known as main thread. So by default every language is multithreaded.
  • In a single threaded program the execution takes place by performing its actions one by one ie suppose it has started calling a method until and unless that method execution is completed it cannot go to the other methods for execution. So if at all a method is taking more time to execute than the expected until that time all the other methods has to wait.
  • to overcome the problems with single threaded applications, multithreading was designed wherein multithreading we can use a separate thread for calling each method.

  • Multithreading has been designed for maximum utilization of CPU resources. So multithreaded applications execute adopting 2 principals.
  1. Time Sharing: Here Operating system allocates time period for each thread for executing the methods and transfers the control to another thread once the time is elapsed giving equal importance for all the threads to execute.
  2. Maximum utilization of resources: This comes into picture only if the first principal violates ie if a thread could not execute in its given time period for some reason without wasting at that thread Operating system transfers the control to the other threads in execution.

How to define an exception class ?

If we want to define an exception class we  need to adopt the following process:
  1. Define a class by inheriting from predefined class exception so that a new class is also an exception.
  2. Overwrite the message properly and provide the error message that has to be given when the exception occurred.
Add a new class DivideByOddNoException and  write the following code:

 
using System;

namespace CSharpConsole
{
    class DivideByOddNoException: Exception 
    {
        public override string Message
        {
            get
            {
                return "Attempted to divide by odd number ";
            }
        }
   }
}
 Add a new class ThrowDemo.cs and write the following code:

using System;

namespace CSharpConsole
{
    class ThrowDemo
    {
        static void Main()
        {
            int x, y, z;
            Console.Write("Enter x value ");
            x = int.Parse(Console.ReadLine());
            Console.Write("Enter y value ");
            y = int.Parse(Console.ReadLine());
            if (y % 2 > 0)
            {
                throw new DivideByOddNoException();
                //throw new ApplicationException("Divisor should not be an odd number ");
            }
            z = x / y;
            Console.WriteLine(z);
            Console.WriteLine("End of program ");
        }
    }
}

Wednesday, 13 March 2013

Exceptions in C#

When we write a program there are chances of coming across 2 different types of errors
  • Compile-time Errors
  • Run-time Errors.
An error which comes into picture at the time of program compilation is a compile-time error which may occur due to syntax mistakes. An error which occurs in the middle of the programs execution is known as a run-time error and these errors occur due to various reasons like wrong implementation of logic, wrong inputs supplied to a program, missing of required resources etc. 
A compile-time error is not dangerous because it occurs at the time of compilation whereas run-time errors are dangerous because they occur in the program when the execution is going on and if at all a run-time error occurs in a program, the program terminates abnormally on the same line where the error gets occured without executing the rest of the code.

Note:  Whenever an error situation occurs in a program the exception handler comes into picture to identify the type of error and then creates an object of an exception class associated with that error and throws that object and that object will first terminate the program abnormally and then displays an error message associated with an exception object.

Exception Handling

Whenever a run- time error occurs under the program abnormal termination is occuring so we will be facing so many problems in the application. To overcome the problems we are provided with a mechanism known as Exception Handling.

Exception Handling is a process of stopping the abnormal termination of the program whenever the exceptions are terminating the program in case of run time error. If we can stop the abnormal termination of a program we will be getting following advantages :
  • We can make the statement which are not associated with the error.
  • We can display a user friendly error message to the end user so that he can resolve the error, got occurred provided if it is in his hands.
  • We can also perform some corrective actions to come out of a problems that may occur due to error.
To handle an exception we need to enclose our code under some special blocks known as try catch block which should be used as following:

try
{
-statements which requires execution when the run time error occurs.
-statements which does not require execution only when the run time error occurs.
}
catch
{
-statements which requires execution only when the run time error occurs.
}
[--< multiple catch blocks if required >--]

Add a class Exception demo.cs and write the following code:


using System;

namespace CSharpConsole
{
    class ExceptionDemo
    {
        static void Main()
        {
            int x, y, z;
            try
            {
                Console.WriteLine("Enter x value: ");
                x = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter y value: ");
                y = int.Parse(Console.ReadLine());

                z = x / y;
                Console.WriteLine(z);
            }
            catch (DivideByZeroException ex1) //ex1 is a variable of class          DivideByZeroException
            {
                Console.WriteLine("Divisor must not be zero");
            }
            catch (FormatException ex2) //ex2 is a variable of class FormatException
            {
                Console.WriteLine("Input must be numeric");
            }

            catch (Exception ex3)
            {
                Console.WriteLine(ex3.Message);//message is a read only property
            }
            {
                Console.WriteLine("End of the program");
                Console.ReadLine();
            }
         }
    }
}