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?