3 Replies - 2426 Views - Last Post: 21 November 2012 - 10:35 AM Rate Topic: -----

#1 amre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-March 12

delete an item in a list from parameter value

Posted 19 November 2012 - 04:35 PM

i am trying to delete the empID that is passed in the parameter from the employee list shown below, i have shown all various attempts at this but after much reading and web searching tutorials i am still unsure how it should work.
errors are mainly that the list has some invalid arguments. any help would be great

private List<Employee> emp;
        public List<Employee> Emp
        {
            get { return emp; }
            set { emp = value; }
        }

public bool RemoveEmployee(int empID)
        {
                  //emp.Remove(emp.Find(item => item= empID));
                //emp.Remove(emp.Find(List<Employee>==  empID));
                //emp.Remove(empID);
                //if (Employee E = emp.Find(E=>E.empID==(empID)))
               // {
               //     emp.Remove(empID);
               // }
               // return true;            
              // emp.Remove(empID);            
            //Employee e = emp.Find(e => e.empID == empID);
            //emp.Remove(e);
           
            return true;
        }



Is This A Good Question/Topic? 0
  • +

Replies To: delete an item in a list from parameter value

#2 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 106
  • Joined: 20-December 11

Re: delete an item in a list from parameter value

Posted 19 November 2012 - 05:00 PM

Can't you foreach loop through your emp list and get the ID? Where is the ID stored?
    public bool RemoveEmployee(int empID)
    {
        foreach (Employee employee in emp)
        {
            if (employee.ID == empID)
            {
                emp.Remove(employee);
            }
        }

        return true;
    }

Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,124
  • Joined: 05-May 12

Re: delete an item in a list from parameter value

Posted 19 November 2012 - 05:18 PM

No you should not and cannot modify the container that you are iterating over using foreach(). If you used a for() loop, you can do the modification. The common pattern though in .NET code is is to use two loops like in this pseudo code:
List<T> toDelete = new List<T>();
foreach(T t in targetList)
{
    if (t is item to be removed)
        toDelete.Add(t);
}

foreach(T t in toDelete)
{
    targetList.Remove(t);
}


This post has been edited by Skydiver: 19 November 2012 - 05:18 PM

Was This Post Helpful? 2
  • +
  • -

#4 amre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-March 12

Re: delete an item in a list from parameter value

Posted 21 November 2012 - 10:35 AM

View PostSkydiver, on 19 November 2012 - 05:18 PM, said:

No you should not and cannot modify the container that you are iterating over using foreach(). If you used a for() loop, you can do the modification. The common pattern though in .NET code is is to use two loops like in this pseudo code:
List<T> toDelete = new List<T>();
foreach(T t in targetList)
{
    if (t is item to be removed)
        toDelete.Add(t);
}

foreach(T t in toDelete)
{
    targetList.Remove(t);
}



thank you, with your help I got it working
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1