Practical Implementation of
interface in C# .Net……………………………
Steps 1 First we will add interface
which name is ICustomer in our Windows Application
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
namespace custmgmt
{
public interface ICustomer
{
int Compare(object ob1, object ob2);
}
}
Steps 2 Second step we add class
which name is ObjectCollection.cs in our Windows Application
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
namespace custmgmt
{
public class ObjectCollection
{
private object[] obar = new object[1];
private int index = -1;
public int GetIndex()
{
return index;
}
//
.........................................
public int Add_Object(object value)
{
index++;
obar[index] = value;
Array.Resize(ref obar, obar.Length +
1);
return index;
}
//...............................................
public object GetValue(int myindex)
{
return obar[myindex];
}
//..............................................
int c;
public int Sort(ICustomer cmp)
{
for (int i = 0; i <index;
i++)
{
for (int j= i+1; j
<=index; j++)
{
c=cmp.Compare(obar[i],obar[j]);
if (c == 1)
swap(ref obar[i], ref obar[j]);
}
}
return c;
}
//..............................................
private void swap(ref object ob1, ref object ob2)
{
object temp = ob1;
ob1 = ob2;
ob2 = temp;
}
//...............................................
public int Delete_Object(int myindex)
{
int flag = 0;
for (int i = 0; i <
obar.Length; i++)
{
if (i == myindex)
{
for (int j = i; j <
obar.Length - 1; j++)
{
obar[j] = obar[j + 1];
}
flag = 1;
break;
}
}
return flag;
}
}
}
Step 3 then third steps we will add class
which name is Customer and implement ICustomer in our Windows Application
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
namespace custmgmt
{
class Customer :ICustomer
{
public static ObjectCollection obj = new ObjectCollection();
private int SrNo;
public int IntSrNo
{
get { return SrNo; }
set { SrNo = value; }
}
private string FirstName;
public string StrFirstName
{
get { return FirstName; }
set { FirstName = value; }
}
private string LastName;
public string StrLastName
{
get { return LastName; }
set { LastName = value; }
}
private DateTime Dob;
public DateTime DateTimeDob
{
get { return Dob; }
set { Dob = value; }
}
public void AddCustomer(Customer cus)
{
obj.Add_Object(cus);
}
public Customer SearchBySrNo(Customer cus)
{
for (int i = 0; i
<=obj.GetIndex(); i++)
{
Customer cus1=(Customer)obj.GetValue(i);
if (cus.SrNo ==
cus1.SrNo)
{
cus.StrFirstName =
cus1.StrFirstName;
cus.StrLastName =
cus1.StrLastName;
cus.DateTimeDob =
cus1.DateTimeDob;
return cus;
}
}
return null;
}
public int UpdateBySrNo(Customer cus)
{
for (int i = 0; i <=
obj.GetIndex(); i++)
{
Customer cus1 = (Customer)obj.GetValue(i);
if (cus.SrNo ==
cus1.SrNo)
{
cus1.StrFirstName =
cus.StrFirstName;
cus1.StrLastName =
cus.StrLastName;
cus1.DateTimeDob =
cus.DateTimeDob;
return 1;
}
}
return 0;
}
public int DeleteBySrNo(int ind)
{
int j=0;
for (int i = 0; i <=
obj.GetIndex(); i++)
{
Customer c1 = (Customer)obj.GetValue(i);
if (c1.IntSrNo == ind)
{
j = obj.Delete_Object(i);
break;
}
}
return j;
}
public int Compare(object ob1, object ob2)
{
Customer cus1, cus2;
cus1 = (Customer)ob1;
cus2 = (Customer)ob2;
if (cus1.IntSrNo >
cus2.IntSrNo)
return 1;
else if (cus2.IntSrNo <
cus2.IntSrNo)
return -1;
else
return 0;
}
public Customer GetValue(int i)
{
Customer cus = new Customer();
cus =(Customer) obj.GetValue(i);
return cus;
}
}
}
Step 4 fourth steps we will code on
Windows From and call all method of class which name is Customer
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
namespace custmgmt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//code for How to Save data
............................
private void btnSave_Click(object sender, EventArgs e)
{
Customer cus = new Customer();
cus.IntSrNo = Convert.ToInt32(txtsrno.Text);
cus.StrFirstName =
txtfname.Text;
cus.StrLastName =
txtlname.Text;
cus.DateTimeDob = Convert.ToDateTime(txtdob.Text);
cus.AddCustomer(cus);
MessageBox.Show("Customer
Added ...");
TextClear();
}
//code for How to sort data
with SrNo ............................
private void btnsort_Click(object sender, EventArgs e)
{
Customer.obj.Sort(new Customer());
MessageBox.Show("Record
Sorted..");
}
int temp;
//code for How retrive
first data from
Array............................
private void btnfirst_Click(object sender, EventArgs e)
{
temp = 0;
Customer cus1;
cus1 = (Customer)Customer.obj.GetValue(temp);
txtsrno.Text =
cus1.IntSrNo.ToString();
txtfname.Text =
cus1.StrFirstName;
txtlname.Text =
cus1.StrLastName;
txtdob.Text =
cus1.DateTimeDob.ToString();
}
int i;
//code for How retrive
last data from
Array............................
private void btnlast_Click(object sender, EventArgs e)
{
Customer cus1;
cus1 = (Customer)Customer.obj.GetValue(Customer.obj.GetIndex());
txtsrno.Text =
cus1.IntSrNo.ToString();
txtfname.Text =
cus1.StrFirstName;
txtlname.Text =
cus1.StrLastName;
txtdob.Text =
cus1.DateTimeDob.ToString();
}
//code for How to clear
textbox control ............................
private void btnClear_Click(object sender, EventArgs e)
{
TextClear();
}
//method for
TextClear.............
private void TextClear()
{
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(TextBox))
{
c.Text = "";
}
}
}
//Code for how to get
Previouse data from Array.............
private void btnprv_Click(object sender, EventArgs e)
{
if (temp>0)
temp--;
Customer cus1;
cus1 = (Customer)Customer.obj.GetValue(temp);
txtsrno.Text =
cus1.IntSrNo.ToString();
txtfname.Text =
cus1.StrFirstName;
txtlname.Text =
cus1.StrLastName;
txtdob.Text =
cus1.DateTimeDob.ToString();
}
//Code for how to get Next data
from Array.............
private void btnNext_Click(object sender, EventArgs e)
{
if (temp < Customer.obj.GetIndex())
temp++;
Customer cus1;
cus1 = (Customer)Customer.obj.GetValue(temp);
txtsrno.Text =
cus1.IntSrNo.ToString();
txtfname.Text =
cus1.StrFirstName;
txtlname.Text =
cus1.StrLastName;
txtdob.Text =
cus1.DateTimeDob.ToString();
}
//Code for how to Search data
from Array with SrNo.............
private void btnSearch_Click(object sender, EventArgs e)
{
Customer cus1=new Customer();
cus1.IntSrNo = Convert.ToInt32(txtsrno.Text);
cus1=cus1.SearchBySrNo(cus1);
if (cus1 != null)
{
txtsrno.Text =
cus1.IntSrNo.ToString();
txtfname.Text =
cus1.StrFirstName;
txtlname.Text =
cus1.StrLastName;
txtdob.Text =
cus1.DateTimeDob.ToShortDateString();
}
else
{
MessageBox.Show("SrNo is not
found");
}
}
//Code for how to delete data
from Array with SrNo.............
private void btnDelete_Click(object sender, EventArgs e)
{
Customer cus1 = new Customer();
int
r=cus1.DeleteBySrNo(Convert.ToInt32(txtsrno.Text));
if (r == 1)
MessageBox.Show("Deleted
Sucessfull.");
else
MessageBox.Show("not
found");
}
//Code for how to update data
from Array with SrNo.............
private void btnUpdate_Click(object sender, EventArgs e)
{
Customer cus1 = new Customer();
cus1.IntSrNo = Convert.ToInt32(txtsrno.Text);
cus1.StrFirstName =
txtfname.Text;
cus1.StrLastName =
txtlname.Text;
cus1.DateTimeDob = Convert.ToDateTime(txtdob.Text);
int i=cus1.UpdateBySrNo(cus1);
if (i == 1)
{
MessageBox.Show("Update
Successfully");
}
else
{
MessageBox.Show("SrNo is not
Found");
}
}
}
}
0 comments:
Post a Comment