Remove object from an Arraylist

Asked

Viewed 479 times

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 its box attribute stores the same value that was typed in mine TextBox.

Why am I mounting a parking system, and when the customer does the checkout and informs his vacancy, the object that has the value of the number of his vacancy will have to be excluded, 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; perhaps the enumeration operation will not be performed.

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

Would anyone have any tips? Do you know 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?

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 the ToList will create a new list (cached) and will not undergo subsequent changes to the collection (the original list has been changed). If by any chance, during the foreach, it is necessary to deal with a new add or even the remove, the new list shall not recognise such amendments.

MSDN DOCUMENTATION

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

1

Error occurs because the collection listVagas was amended during the foreach. Create a temporary collection and add the objects you want to:

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);
}

0

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

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

0

You can use a lambda expression

int idConsulta = Convert.ToInt32(consulta_box.Text);
listVagas = listVagas.Where(x=>x.getBox() != idConsulta).ToList();
  • This works with ArrayList?

  • To do using arrayList it would be necessary to first convert The Arraylist into an Array and then into a list. It wouldn’t be very performative, I hadn’t noticed that Oce was using Arraylist. As the bigown commented in his question, it would be interesting to work directly with a List, since the Arraylist is obsolete...

  • It’s not my question, just for the record.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.