Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After removing elements from arraylist,its not effecting to arraylist.
when retrieving the arraylist elements ,then again its showing the deleting elements.
How to reslove this problem?

What I have tried:

C#
public class BaseEmployee
    {
        public int Id;
        public string Name;
    }

    public class Employee : BaseEmployee
    {
        public string Address;



        public Object Add()
        {

            Employee employeeDetail = new Employee();
            Console.Write("Employee Id:");
            employeeDetail.Id = int.Parse(Console.ReadLine());

            Console.Write("Employee Name:");
            employeeDetail.Name = Console.ReadLine();
            Console.Write("Employee Address:");
            employeeDetail.Address = Console.ReadLine();
            return employeeDetail;

        }

        static void Main(string[] args)
        {

            ArrayList arraylist = new ArrayList();
          


            Employee employeeDetails = new Employee();

            bool p = true;

            while (p)
            {

                Console.WriteLine("Employee Record Management System\n");
                Console.WriteLine("\n1)Add");
                Console.WriteLine("\n2)Edit");
                Console.WriteLine("\n3)Delete");
                Console.WriteLine("\n4)Dispaly");
                Console.WriteLine("\n5)Exit");
                Console.Write("\n Select opertaion:");
                int Option = int.Parse(Console.ReadLine());

                switch (Option)
                {
                    case 1:
                        arraylist.Add(employeeDetails.Add());

                        break;


                    case 4:
                        foreach (Object obj in arraylist)
                        {

                            Employee emp = obj as Employee;
                            Console.Write("{0}\t\t{1}\t\t{2}", emp.Id, emp.Name, emp.Address);
                            Console.Write("\n");
                            //Console.WriteLine(obj);
                        }


                        break;
            case 3:Console.Write("Enter  Employee Id:");
                        int EId = int.Parse(Console.ReadLine());
                        foreach(Object obj in arraylist)
                        {
                            Employee emp = obj as Employee;
                            if(EId==emp.Id)
                            {

                                arraylist.Remove(emp.Id);
                                arraylist.Remove(emp.Name);
                                arraylist.Remove(emp.Address);
                               

                            }
                        }

                          break;



                }

            }
        }

    }
}
Posted
Updated 13-Oct-16 6:32am

You should remove the object not the properties of the object :
C#
...
          arraylist.Remove(emp);
...
 
Share this answer
 
Comments
F-ES Sitecore 13-Oct-16 4:21am    
I agree, not sure if you'll need arraylist.Remove(obj) rather than "emp" though. You'll also need a "break;" after the Remove line.
Mehdi Gholam 13-Oct-16 4:24am    
Presumably emp is not null after the cast so you are ok in the if block, but yes!
CPallini 13-Oct-16 4:23am    
5.
Mehdi Gholam 13-Oct-16 4:24am    
Cheers!
I suggest you to use List<Employee> instead of ArrayList.
You'll avoid using Object (casting it every time) and your code will be easier to read.
Moreover you'll be able to use the method RemoveAll:

arraylist.RemoveAll(emp => emp.Id == EId);

I rewritten your code fragment, so you can evaluate my advice:

C#
public class BaseEmployee
    {
        public int Id;
        public string Name;
    }
 
    public class Employee : BaseEmployee
    {
        public string Address;

        public static Employee Add()
        {
 
            var employeeDetail = new Employee();
            Console.Write("Employee Id:");
            employeeDetail.Id = int.Parse(Console.ReadLine());
 
            Console.Write("Employee Name:");
            employeeDetail.Name = Console.ReadLine();
            Console.Write("Employee Address:");
            employeeDetail.Address = Console.ReadLine();
            return employeeDetail;
 
        }
 
        static void Main(string[] args)
        {

            var arraylist = new List<Employee>();
          
            //Employee employeeDetails = new Employee();
 
            bool p = true;
 
            while (p)
            {
 
                Console.WriteLine("Employee Record Management System\n");
                Console.WriteLine("\n1)Add");
                Console.WriteLine("\n2)Edit");
                Console.WriteLine("\n3)Delete");
                Console.WriteLine("\n4)Dispaly");
                Console.WriteLine("\n5)Exit");
                Console.Write("\n Select operation:");
                int Option = int.Parse(Console.ReadLine());
 
                switch (Option)
                {
                    case 1:
                        arraylist.Add(Employee.Add());
 
                        break;
 

                    case 4:
                        foreach (Employee emp in arraylist)
                        {
 
                            //Employee emp = obj as Employee;
                            Console.Write("{0}\t\t{1}\t\t{2}", emp.Id, emp.Name, emp.Address);
                            Console.Write("\n");
                            //Console.WriteLine(obj);
                        }
 

                        break;
                    case 3:
                        Console.Write("Enter  Employee Id:");
                        int EId = int.Parse(Console.ReadLine());
                        
                        arraylist.RemoveAll(emp => emp.Id == EId);
                        
                        break;
                }
 
            }
        }
 
    }


Bye
 
Share this answer
 
If you're going to be changing the list, you can't do it inside a foreach loop.

My advice is to use linq to find it, and then remove it if found.
C#
var found = arraylist.Where(x=>x.Id == EId).FirstOrDefault();
if (found != null)
{
    arraylist.Remove(found);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900