Tuesday, February 25, 2014

Exception & WINDOWS PROGRAMMING

Que. How can we raise the exception?
Ans. If we want to raise an exception follow the blow process –
  • Create an object of exception class.
  • Throw that object using Throw object.
  •            Syntax – throw <object>
  • When we want to raise an exception explicitly, we can adopt to different approach

 a.  Where we want to raise an exception than create an object of class ApplicationException by passing an error massage as a parameter to its constructor
ApplicationException (string error massage)

Eg:-
ApplicationException  obj = new ApplicationException (“< error massage>”)

throw obj;
or
throw new ApplicationException (“< error massage>”)


.      b. First define a user define exception class as par your requirement and use that class in the place of application exception class same as the above.

Q. How to define an Exception class?
Ans. to define our own exception classes about the blow process-
  • Define a class inheriting from the pre-defined class Exception so that the new class also becomes an Exception class.
  • Now override the virtual readonly property of Exception class under your new class as par your requirement.

- Add a class ThrowDemo.cs and write the following
    class OddNumberException : Exception
    {
        public override string  Message


     {
     get
     {
            return "odd number can't be used as Divisor";
     }
}
         
    }
   
    class ThrowDemo
    {
        static void Main()
        {
            try
            {
                int x, y, z;
                Console.Write("Enter x Value :");
                x = int.Parse(Console.ReadLine());
                Console.Write("Enter y Value :");
                y = int.Parse(Console.ReadLine());
                z = x / y;
                if (y % 2 > 0)
                {
                    //throw new ApplicationException("divisor can't be odd number");
                    throw new OddNumberException();
                }
                Console.WriteLine(z);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine("End the Program");
            Console.ReadLine();
        }
    }
} 

Partial Class

It’s a new approach that came into picture from C# 2.0 which allows splitting a class into multiple files i.e. one class can be defined with in more than one file
-To define ‘partial class’ we need to prefix the class with “partial” modifier.

- If we want to inherit a partial class from any other class it is enough to inheriting any single file      but not all.
- Partial class is allowing multiple programmers to work on the same class at same time but on different files.
- Partial classes are used for physical separation of related code into different file, but logically they will be under the same class.

- Add a class Part1.cs and write the following:
using System;
namespace oopsProject
{
    public partial class Parts
    {
        public void Method1()
        {
            Console.WriteLine("Method 1");
        }
        public void Method2()
        {
            Console.WriteLine("Method 2");
        }
    }
}

- Add a class Part2.cs and write the following:
using System;
namespace oopsProject
{
    public partial class Parts
    {
        public void Method3()
        {
            Console.WriteLine("Method 3");
        }
        public void Method4()
        {
            Console.WriteLine("Method 4");
        }
    }
} 

- Add a class TestParts.cs and write the following:
using System;
namespace oopsProject
{
    class TestParts
    {
        static void Main()
        {
            Parts p = new Parts();
            p.Method1(); p.Method2(); p.Method3(); p.Method4();
           
            Console.ReadLine();
        }
    }
} 

WINDOWS PROGRAMMING

            In development of any application we have required a user interface (UI) to communicate with end user. We have two different types of User Interface
  1. CUI ( Character User Interface)
  2. GUI ( Graphical User Interface)
- Traditionally we have CUI eg: dos, UNIX, os etc. This application suffers from little criticism          like:
1. They are not user-friendly. As we need to learn the commands first to use them.
2. They do not allow navigating from one place to other place.
- To solve the above problems in early 90’s GUI applications are introduced by Microsoft with its windows operation system, which has a beautiful feature known as “Look & Feel “ To develop them Microsoft has introduced an language also into the market in 90’s i.e. VB , letter when .net was introduced the support for GUI has been given in all .net languages.
- To develop windows application we required a set of component known as controls provided as classes to us, with in the namespace system.windows.forms
- Controls are categorized into 2:
1. Container Controls: - these controls are capable of holding other controls on them eg: Form, Panel, GroupBox, SplitContainer, TabControl etc.

2. Non-Container Controls: - These controls are not capable of holding any controls on them moreover they can be used only after being placed on a container. Eg: Button, Label, TextBox, CombBox, ChekBox, TreeView, ListView, DataGridView etc. 

No comments: