Sunday, 21 July 2013

Nongeneric Collection:ArrayList By:Tanuj Kumar

Nongeneric Collection:ArrayList

Collection in .net with proper coding.

Arraylist:

Nongeneric Collection:ArrayList


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;
using ClassLibrary3;
using System.Collections;

namespace collectionarraylist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ArrayList arr = new ArrayList();
        int i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj.Id = int.Parse(textBox1.Text);
            obj.Name = textBox2.Text;
            arr.Add(obj);
            label3.Text = "saved";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (arr.Count > 0)
            {
                Class1 obj = new Class1();
                obj = (Class1)arr[0];
                textBox1.Text = (obj.Id).ToString();
                textBox2.Text = (obj.Name);
            }
            else {
                label3.Text = "no record is saved";
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show (arr.Count.ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (arr.Count > 0)
            {
                Class1 obj = new Class1();
                int a = arr.Count;
                obj = (Class1)arr[a-1];
                textBox1.Text = (obj.Id).ToString();
                textBox2.Text = (obj.Name);
            }
            else
            {
                label3.Text = "no record is saved";
            }
        }
        private void textBox1_TextSelect(object sender, EventArgs e)
        {
            label3.Text = "";
       
       
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label3.Text = "";
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            label3.Text = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            if (arr.Count > 0)
            {
                int a = int.Parse(textBox3.Text);
                if (arr.Count < a)
                {
                    obj = (Class1)arr[a - 1];
                    textBox1.Text = (obj.Id).ToString();
                    textBox2.Text = (obj.Name);
                }
                else { label3 .Text  = "No record is saved"; }

            }
            else { label3.Text = "No record is saved"; }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            label3.Text = "";
        }
    }
}

Arraylist searching by id:
This is button click coding:
{
            Class1 obj = new Class1();
            int a = int.Parse(textBox4.Text);
            for (i = 0; i <= arr.Count; i++)
            {
                obj = (Class1)arr[i];
                if (a == obj.Id) {

                    textBox1.Text = obj.Id.ToString();
                    textBox2.Text = obj.Name;
}
Classlibrary3:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary3
{
    public class Class1
    {
        int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }}

0 comments:

Post a Comment