Tuesday, February 16, 2016

Polymorphism in c # with example



Ability to take more than one form is called polymorphism. The polymorphism consist two words poly and morphism poly means “multiple” and morphism means “form”.
Following are the types of polymorphism

-Compile time polymorphism also called Early binding or overloading or static binding

-Run time polymorphism also called late binding or overriding or dynamic binding

Compile time polymorphism – In compile time polymorphism method name is same with different parameter (signature)
The example of the compile time polymorphism is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace compile_time_poly
{
    public class Class1
    {
        public void show(int a, int b)
        {
            MessageBox.Show((a + b).ToString());
        }
        public void show(int a, int b, int c)
        {
            MessageBox.Show((a + b + c).ToString());
        }
    }
}

Code on button click:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace compile_time_poly
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.show(5, 10);
            obj.show(5, 10, 15);
        }
    }
}


Output:
15
30
Run time polymorphism – In run time polymorphism method name and parameter should be same. Run time polymorphism is achieved by virtual and override keyword. The example of run time polymorphism is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace compile_time_poly
{
    public class Class1
    {
        public virtual void show(int a, int b)
        {
            MessageBox.Show((a + b).ToString());
        }
        public class class2 : Class1
        {
            public override void show(int a, int b)
            {
                MessageBox.Show((a + b).ToString());
            }
        }
    }
}

Button click code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace compile_time_poly
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.show(5, 10);
            obj.show(5, 15);
        }
    }
}


Output:
15
20




Saturday, February 13, 2016

Maintaining Data Integrity in Sql

Data integrity tells about marinating the proper data. To maintain proper data we need to impose some business rules on data. The business rules are provide in the from at data integrity constraints which are going to restrict us,  If we don't maintain the integrity.
  1. Not null
  2. Unique
  3. Primary Key
  4. Default
  5. Check
  6. Foreign Key
1 -  Not Null

If this constraint is imposed on a column it will not allow null values on it. We can impose 'n' number of not null constraints on a table.

Syntax

Create Table <Table Name> (Col1 <Data Type>[Width][Not Nulll],Col2 <Data Type>[Width][Not Nulll],.........................Coln <Data Type>[Width][Not Nulll]

Example

Create Table Employee(empid int Not Null, ename varchar(50), salary decimal(7,2)  Not Null

If you try to insert values in Employee table like

Insert into Employee(101,'Sharad',NULL) ->ERROR

Note

The draw of Not Null constraints is even if it restricts null values. It will also allow duplicate values.

2 -  Unique

If this constraint is impose on a column, It will not allow the duplicate values.

Example

Create Table Employee(empid int Unique, ename varchar(50), salary decimal(7,2)  Not Null

Note
  • The not null constraint is a part of the table, which  stores along with column in the table and when constraint is violated it will display us the name of the constraint on which the violation has been done.
  • The rest of constraints are separated object in the database apart from table and linked up the column because they are separated jobs they have their own name for identification. So if these constraints are violated they display their own identity but not the name of column.
  • So when this constraint are violated using the name only we need to identity where the violation has been done. Now follow the following convention and provide name to constraints.
Example

Create Table Employee(empid int constraint empid Unique, ename varchar(50), salary decimal(7,2)  Not Null

A constraint can be defined or apply in 2 ways
  1. Column level definition
  2. Table level definition
1 - Column level definition

In this case the details of constraints will provide immediately after column details as if in our previous example.

2 - Table level definition

In this case first all the columns are define, and in the end we define the constraint of the column.

Example

Create Table Employee(empid int , ename varchar(50), salary decimal(7,2)  Not Null, constraint empid_uq Unquie(empid))

Note

There is no different in behavior however we define the constraint either in table level or column level but the advantage is it, A constraint is defined at table level we can go for a composite constraint that is single on multiple columns. Basically the composite unique constraint checks the uniqueness the combination of columns but not single column.

Example

Create Table Employee_Deatils(city_code varchar(10),area_code varchar(10), address varchar(40), constraint cc_ac_uq  unique (city_code,area_code))

Note

The drawback of the unique constraint is it allow null value on it.

3 - Primary Key

This constraint is a combination of unique and not null, which restricts duplicate values as well as null values also. A table can have only one primary key constraint present on it, which can be either single column, or multiple columns.

Example

Create Table Employee(empid int constraint eid primary key, ename varchar(50), salary decimal(7,2)  Not Null

Composite Primary Key Example

Create Table Employee_Deatils(city_code varchar(10),area_code varchar(10), address varchar(40), constraint cc_ac_pkprimary key(city_code,area_code))

4 - Default Constraint

The default value of any column in a table is null unless a not null constraint or primary key imposed on it. We can change the default value of a column according to our need by using default constraint. The value which is specified as a default value comes into the picture when no value is specified to the column while inserting data into table.

Example

Create Table Employee(empid int , ename varchar(50), salary decimal(7,2)  default 2000)

When you insert a row into table and does not provide salary column value it automatically insert 2000 in salary column.

5 - Check Constraint

This is used for checking the value within a common to be according specification as following
  • Check (salary>=2000)
  • Check (salary >= 2000 And salary<=5000)
  • Check (salary ==2000 Or salary=5000)
Example

Create Table Employee(empid int , ename varchar(50), salary decimal(7,2)  Not Null constraint sal_ck Check(salary>=30000))

6 - Referential Integrity Constraint

This constraint is used for establishing master child relation between tables. To establish the relationship we require the following.
  • Master Table  :-  Which is going to contain the initial information in it.
  • Child Table : - Which should contain the detail information in it.
  • A reference key column in the master table which provide the reference values for the child table. This provides the reference values for the child table. This reference key column should contain either a primary key constraint or a unique constraint on it.
  • A foreign key column on a child table, which should restrict the values into the column, that are not present in the reference key column for doing this it has been imposed with a foreign key constraint on it.
     

Friday, February 12, 2016

What is constructor in C#?



Constructor is special kinds of method which is automatically call when we create the instance or object of the class. It is also responsible for the object initialization and memory allocation of class.
Constructor name and class name should be same.

Constructor don’t return any value

We cannot inherited to the constructor

Constructor always public

Example:
Class class1
{
Public class1()
}
Types of constructor
The types of Constructor are given below:
1. Default constructor
2. Parameterized constructor
3. Private constructor
4. Copy constructor
5. Static constructor



Default constructor – A constructor without any parameter is called default constructor , in default constructor every object of the class will be initialized without any value the example of the default constructor is given below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public class Class1
    {
        public string s,s1;
        public Class1()
        {
            s = "welcome";
            s1 = "Dot Net darpan";
        }
      
       
    }
}
.cs code - 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Class1 obj = new Class1();
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(obj.s);
            MessageBox.Show(obj.s1);
        }
    }
}


output - 





Parameterized constructor – The constructor which have at least one parameter is called parameterized constructor .The example of the parameterized is given below: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public class Class1
    {
        public int i, j;
        public Class1(int x, int y)
        {
            i = x;
            j = y;
          
        }
      
       
    }
}
.cs code  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Class1 obj = new Class1(10,20);
        private void button1_Click(object sender, EventArgs e)
        {
            int a = obj.i;
            int b = obj.j;
            MessageBox.Show(a.ToString());
            MessageBox.Show(b.ToString());

        }
    }
}


output :
10
20



Copy constructor – The constructor which creates an object by copy variable from other that type of constructor is called copy constructor .the main purpose of the copy constructor is to initialize new object to the value of an existing object. The syntax of the copy constructor is given below:
Public class class1
{
int I,j;
public class1(class1 o)
{
I=o.i;
J=o.j;
}
}
Class1 obj1=new obj1(10,20)
Class1 obj2=new obj2(obj1)
}
Static constructor – A static constructor is invoked only once for all instance of the class and it is invoked when we create the first instance of the class .The static constructor is used to initialize static field of the class the write the code which is executed only once.
Key points of static constructor
A static constructor does not contain any access modifier and also parameter less.
Static constructor is always public.
It cannot be call directly.
The syntax of the static constructor is given below:
Public class class1
{
Static class1()
{
Messagebox.show(“Static constructor”);
}
}

Private constructor – Private constructor is used to initialized the static member of the class .when a constructor is private , it is not possible for other classes to derive from this class.The sysntax of the private constructor is given below :
Public class class1
{
Private class1()
}