santi000001
santi000001

Reputation: 21

Search in an ArrayList C#

class EmployeeDAL
{
    private ArrayList _employees;

    public EmployeeDAL()
    {
        _employees = new ArrayList();
        _employees.Add(new Employee { EmployeeID = 1, EmployeeName = "Ram", Salary = 50000 });
        _employees.Add(new Employee { EmployeeID = 2, EmployeeName = "Sahaym", Salary = 40000 });
        _employees.Add(new Employee { EmployeeID = 3, EmployeeName = "Gopi", Salary = 55000 });
        _employees.Add(new Employee { EmployeeID = 4, EmployeeName = "Prakash", Salary = 45000 });
        _employees.Add(new Employee { EmployeeID = 5, EmployeeName = "Dheeraj", Salary = 60000 });
        _employees.Add(new Employee { EmployeeID = 6, EmployeeName = "Shibhu", Salary = 50000 });
    }

    public bool DeleteEmployee(int id)
    {
        if (_employees.Contains(id))
        {
            _employees.Remove(id);
            return true;
        }
        else
            return false;
    }
}

I want to delete an employee with specific id using DeleteEmployee(id) method. How can I do this in ArrayList ?

Upvotes: 1

Views: 188

Answers (2)

tobeypeters
tobeypeters

Reputation: 484

Forgive the crudeness of my code. But, sample of using a List follows:

using System;
using System.Collections.Generic;
using Gtk;

public partial class MainWindow : Gtk.Window
{
    public class Employee
    {
        public int EmployID;
        public string EmployeeName;
        public int Salary;

        public Employee(int v1, string v2, int v3)
        {
            this.EmployID = v1;
            this.EmployeeName = v2;
            this.Salary = v3;
        }
    }

    public class Employees
    {
        public List<Employee> employees = null;

        public bool Delete(int inID)
        {
            Employee buffer = employees.Find(x => x.EmployID == inID);
            if (buffer != null)
            {
                employees.Remove(buffer);
                return true;
            }

            return false;
        }
    }

    public Employees Listof = new Employees();

    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        Build();

        Listof.employees = new List<Employee>()
        {
            new Employee(1, "Ram", 50000),
            new Employee(2, "Sahaym", 40000),
            new Employee(3, "Gopi", 55000),
            new Employee(4, "Prakash", 45000),
            new Employee(5, "Dheeraj", 60000),
            new Employee(6, "Shibhu", 50000)
        };

        label1.Text = "Employee Count: " + Listof.employees.Count.ToString();
    }

    protected void OnDeleteEvent(object sender, DeleteEventArgs a)
    {
        Application.Quit();
        a.RetVal = true;
    }

    protected void OnButton3Pressed(object sender, EventArgs e)
    {
        label1.Text = $"Deleted Employee 3 successful : {Listof.Delete(3)}" +
            " Employee Count: " + Listof.employees.Count.ToString();
    }

Upvotes: 0

sayah imad
sayah imad

Reputation: 1553

Hi as an answer to your question you can use the code below :

public bool DeleteEmployee(int id)
{
        var employees = _employees.Cast<Employee>()
            .Where(e => e.EmployeeID == id)
            .Distinct();

        if (employees.Count() == 0)
            return false;
        else
            foreach (var employee in employees.ToList())
                _employees.Remove(employee);
        return true;
 }

But in my opinion, if it's possible, you should use another type of collection like a List, it could be easier to handle than an ArrayList.

Upvotes: 1

Related Questions