Remove object from an ArrayList

2

I have an arraylist of objects, and I need to remove an object from it as follows.

I need this object to be removed if the 'box' attribute of it stores the same value that was typed in my TextBox .

Why am I setting up a parking system, and when the client checks out and informs them of their vacancy, the object that has the value of the vacancy number will have to be deleted, I tried as follows.

foreach (cadastro cad in listVagas)
{
    if(cad.getBox() == Convert.ToInt32(consulta_box.Text))
    {
        listVagas.Remove(cadOBj);                    
    }
}

but it gives me an error when I click the button to checkout, in the error it points to the foreach, the error is as follows

  An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll   Additional information: Collection has been modified; the enumeration operation may not be performed.

The error I think is why I have to inform the index of the object in the vector, and I already searched in Google but did not find, how to do for an object that has the value of a specific attribute to return its index. p>

Would anyone have any tips? Do you know of any other way to remove this object? Or how to get around this error? Or how to find the index of the object that stores the specified value in a given attribute?

    
asked by anonymous 07.12.2016 / 15:34

4 answers

3

You can try adding the ToList

foreach (cadastro cad in listVagas.ToList())
        {
            if(cad.getBox() == Convert.ToInt32(consulta_box.Text))
            {
                listVagas.Remove(cadOBj);                    
            }
        }

WHY IT WORKS

Simply ToList will create a new list (cached) and will not undergo subsequent changes to the collection (the original list has changed). If, for some reason, a new foreach or even add has to be dealt with on an occasional basis during the remove , the new list will not recognize those changes.

MSDN DOCUMENTATION

The ToList <TSource> ( IEnumerable <TSource> ) method forces the immediate query evaluation and returns a <T> List that contains the query results. You can attach this method to your query to obtain a cached copy of the query results.

    
07.12.2016 / 15:54
1

The error occurs because the listVagas collection was changed during foreach . Create a temporary collection and add the objects you want to it:

var listaTemp = new List<seuObjeto>();

foreach(var objeto in listaReal){
   if(!suaCondicao)
     listaTemp.add(objeto);
}

Another solution is to use a for

for(var i = listaReal.Count - 1; i>=0 ;i--){
  if(suaCondicao)
    listaReal.RemoveAt(i);
}
    
07.12.2016 / 15:42
0

You can use a lambda expression

int idConsulta = Convert.ToInt32(consulta_box.Text);
listVagas = listVagas.Where(x=>x.getBox() != idConsulta).ToList();
    
07.12.2016 / 16:05
0

You can try to use a for iterating from the last position to the first.

for(int i = listVagas.Count-1;i >=0; i--)
{
 if(listVagas[i].getBox() == Convert.ToInt32(consulta_box.Text))
    {
        listVagas.RemoveAt(i);                    
    }
}
    
07.12.2016 / 21:37