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?
This works with
ArrayList
?– Jéf Bueno
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...
– Andre.Santarosa
It’s not my question, just for the record.
– Jéf Bueno