Thursday, 21 March 2013

Cascading Style Sheet (CSS)

CSS is recommended to design the layout of the page including position of the element, appearance of the element n HTML. CSS can  be specified as inline CSS, Internal CSS and External CSS.
  • Inline CSS : Specifying a CSS style by string style attribute of HTML elements without defining it separately is called as Inline CSS. 
Eg: The following example changes font and color of the heading level h1 using Inline CSS.

<!doctype html>
<html>
<body>
<h1 style= "font-family:arial black; color:blue">
Inforica Pvt lmd
</h1>
</body>
</html>
  • Internal CSS : When CSS styles are defined within the head section then it is called as Internal CSS. While defining CSS in the head section. CSS style will have three main parts- Selector, Property and Value. Selector is used to specify the HTML element for which you are defining the style. property is used to specify the CSS property you want to set and value is used to specify the value for CSS property. Property and its value must be separated by colon and multiple properties must be separated by semicolon.
Eg: The following example defines styles for the elements h1 and h2 as internal CSS.

<!doctype html>
<html>
<head>
<style type = "text/CSS">
<h1>
{
font-family:arial black; color:blue"
}
<h2>
{
font-family:times new roman; color:red"
}
</style>
</head>
<body>
<h1> Inforica Pvt lmt </h1>
<h2> Inforica Pvt lmt </h2>
<h1> Inforica Pvt lmt </h1>
<h1> Inforica Pvt lmt </h1>
</body>
</html>
  • External CSS : Defining the CSS styles in seperate files with extention .CSS is called as external CSS. Advantages of external CSS is that they can be used in any HTM document. To add the reference the External CSS file use style element with src attributes or the link element withen the head section.
Syntax:  <style src = "Filename.css">
             <link rel = "stylesheet" type="text/css">
             href="Filename.css"


Partial Classes

  • They are introduced in C# 2.0 which allows us to define a class on multiple files ie we can physically split a class and put it into different files.

          parts.cs                                                          parts1.cs
         class parts                                                       partial class part{
        {                                                                      method1()
         method1()                                                       method2()
         method2()                                                       }
         method3()                                                      parts2.cs
        method4()                                                       partial class parts{                                           
        }                                                                    method3()
                                                                              method4()
                                                                              }
  • In the above case, the class which is defined in left hand side is redefined using the approach of partial classes wherein in this approach the class is physically separated parts logically it is one single unit ie in both the two cases when we create object of class arts we can access all the methods of the class.
  •  if  we want to define partial class in all the files the class name must be same to tell that this is one class only and moreover we need to use a partial modifier on the class in each and every file.
  • In case of partial class if we want to inherit the class from any other class it will be sufficient if we perform inheritance in one file so that it takes to a complete class
The advantages if defining partial classes are:
  • It allow splitting huge volumes of code into separate files so that managing becomes much easier.
  • Multiple programmers can work on the same class at the same time.

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 ");
        }
    }
}