0

I'm trying to remove duplicates of one arraylist filled with the objects "DataPerLabel".

DataPerLabel contains the following methods: getLabelname(), getLabelAdress() and getDataType().

Some background information:

The values labelname, labelAdress and dataType can be set as 1 object in an arraylist named allData by a submit button. When submit is pressed for the 2nd time i want to delete that object of the arraylist.

Picture: Example

Some of the code i've tried:

if (submitButtonClicked == true) {
  if (MessageBox.Show("This is item is already set. Do you want to delete?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
    foreach(DataPerLabel item in allData) {
        if (item.getDataType().Equals(dataType) && item.getLabelAdress().Equals(adress) && item.getLabelName().Equals(label)) {
          allData.Remove(item);
        } else {

        }
    }
  } else {
    //no
  }

}

With this code i'm getting the following error: An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll.

Additional error information: The collection has been changed.

1

3 Answers 3

3

You are trying to iterate in allData list and you are also trying to modify the same list. You can keep removing datas in an other list(eg: removeItems) and then you can remove items. I mean :

   if (submitButtonClicked == true) {
      if (MessageBox.Show("This is item is already set. Do you want to delete?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
List<DataPerLabel> removeItems = new List<DataPerLabel>();
        foreach(DataPerLabel item in allData) {
            if (item.getDataType().Equals(dataType) && item.getLabelAdress().Equals(adress) && item.getLabelName().Equals(label)) {
              removeItems.Add(item);
            } else {

            }
        }
      } else {
        //no
      }

    foreach(DataPerLabel removeItem in removeItems ){
        allData.Remove(removeItem);
      }

}
Sign up to request clarification or add additional context in comments.

2 Comments

It would be a bit simplier without second foreach but with: allData.RemoveRange(removeItems);
I've used this method, works for me. Thank you all of you!
0

As OnurBulbul said you get an exception due trying to modify the list you are iterating through. There are a lot of different solution could be. My suggestion is to use LINQ:

var sortedList = from item in allData
                 where !(item.getDataType().Equals(dataType) && item.getLabelAdress().Equals(adress) && item.getLabelName().Equals(label))
                 select item

It will create a new arrayList for you with all sorted out data

Comments

0
        //Here is just an example of list of array 
       List<object> obj = new List<object>();
        obj.Add(5);
        obj.Add(4);
        obj.Add("we");
        obj.Add(5);
        List<object> objTodelete = new List<object>();

        foreach (var item in obj)
        {
            int count = obj.Count(a => a.ToString() == item.ToString());
            //for finding the dublicate occurence
            if (count > 1)
            {
                //for removing that dublicated occurence.
                object Dublicate = obj.Find(m => m.ToString() == item.ToString());
                if (!objTodelete.Contains(Dublicate))
                {
                    objTodelete.Add(Dublicate); 
                }
            }
        }
        foreach (var itemDublicate in objTodelete)
        {
            obj.Remove(itemDublicate);
        }

5 Comments

First make a loop for occurrence and then find the index of the occurence and then remove.
Why first check if there is any object with Count and then query again if so? You could just check if var item = obj.FirstOrDefault(a => a.ToString() == item.ToString());, after which you can just check if (item != null) { and obj.Remove(item);.
It will cause the same exception as author's 'System.InvalidOperationException'. It is not possible to modify object during iterating through it
Thanks for ur answer, got it now.
yes u r right! For first loop add the duplicate item to another list and the remove that another list from main list. or you can use for loop instead of Foreach. This will not throw 'System.InvalidOperationException'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.