Tuesday, 6 March 2012

Array with Structure and Array Resize Concept in C# .Net

Note: In this program we will discuss about Array According to Array  definition “Array  is collection of similar type data  means homogeneous elements  but  here we will discuss How to insert  heterogeneous elements in Array for this we have concept Array with Structure  and another concept within this program how to Resize Array in run time because when we insert less number of  elements from the size  of array which we declare at the time  of  array declaration then some memory will be wasted ………………..


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 cms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// declare structure for insert heterogeneous elements
struct customer
{
public int cusid;
public string fname;
public string lname;
public DateTime dob;
}
// declare array with one  block because we resize array at run time
customer[] acus = new customer[1];
int index = 0;
//int count = 0;
//code for add new heterogeneous elements in array and resize array size…
private void btnadd_Click(object sender, EventArgs e)
{
acus[index].cusid = Convert.ToInt32(txtid.Text);
acus[index].fname = txtfname.Text;
acus[index].lname = txtlname.Text;
acus[index].dob = Convert.ToDateTime(txtdob.Text);
index++;
Array.Resize(ref acus, index + 1);
MessageBox.Show("data saved");
txtid.Text = "";
txtfname.Clear();
txtlname.Text = String.Empty;
txtdob.Text = "";




}
//code for search data with id
private void btnsearch_Click(object sender, EventArgs e)
{
bool chk = false;
for (int i = 0; i < index; i++)
{
if (txtid.Text == acus[i].cusid.ToString())
{
txtfname.Text = acus[i].fname;
txtlname.Text = acus[i].lname;
txtdob.Text = acus[i].dob.ToShortDateString();
chk = true;
break;
}
}
if (chk == false)
{
MessageBox.Show("not found");
}
}
//code for close From
private void btnclose_Click(object sender, EventArgs e)
{
this.Close();
}
//code for clear textboxs
private void btnclear_Click(object sender, EventArgs e)
{
txtid.Text = "";
txtfname.Text = "";
txtlname.Text = "";
txtdob.Text = "";
}
//code for delete element from array with id
private void btndelete_Click(object sender, EventArgs e)
{
for (int i = 0; i < index; i++)
{
if (txtid.Text == acus[i].cusid.ToString())
{
for (int j = i; j < index; j++)
{
acus[j] = acus[j + 1];
index--;

}
Array.Resize(ref acus, index + 1);
MessageBox.Show("Delete");
return;
}
}
}
//code get First element from Array
private void btnfirst_Click(object sender, EventArgs e)
{
txtid.Text = acus[0].cusid.ToString();e
txtfname.Text = acus[0].fname;
txtlname.Text = acus[0].lname;
txtdob.Text = acus[0].dob.ToShortDateString();

}
//code get last element from Array
private void btnlast_Click(object sender, EventArgs e)
{
txtid.Text = acus[index - 1].cusid.ToString();
txtfname.Text = acus[index - 1].fname;
txtlname.Text = acus[index - 1].lname;
txtdob.Text = acus[index - 1].dob.ToShortDateString();
}
//code get  Next element from Array
private void btnnext_Click(object sender, EventArgs e)
{
int nxt = 0;
for (int i = 0; i < index; i++)
{
if (txtid.Text == acus[i].cusid.ToString())
{
nxt = i + 1;
}
}
if (nxt < index)
{
txtid.Text = acus[nxt].cusid.ToString();
txtfname.Text = acus[nxt].fname;
txtlname.Text = acus[nxt].lname;
txtdob.Text = acus[nxt].dob.ToShortDateString();
}
else
{
MessageBox.Show("data not found");
}
}
//code get  Previouse elements from Array
private void btnprevious_Click(object sender, EventArgs e)
{
int pre = 0;
for (int i = 0; i > index; i--)
{
if (txtid.Text == acus[i].cusid.ToString())
{
pre = i - 1;
}
}
if (pre > 0)
{
txtid.Text = acus[pre].cusid.ToString();
txtfname.Text = acus[pre].fname;
txtlname.Text = acus[pre].lname;
txtdob.Text = acus[pre].dob.ToShortDateString();
}
else
{
MessageBox.Show("data not found");
}
}
//code for update  element from Array with Id
private void btnupdate_Click(object sender, EventArgs e)
{
for (int i = 0; i < index; i++)
{
if (acus[i].cusid == Convert.ToInt32(txtid.Text)) ;
{


acus[index].cusid = Convert.ToInt32(txtid.Text);
acus[index].fname = txtfname.Text;
acus[index].lname = txtlname.Text;
acus[index].dob = Convert.ToDateTime(txtdob.Text);

} MessageBox.Show("updated");
}
}
}
}





1 comments: