1

Case:

I'm adding objects to my arraylist, before i add the object to my arraylist i check if the same does not exist already. After that I try to rewrite the complete arraylist to the text file.

Code:

WaardeObjecten obj = new WaardeObjecten(index.ToString(), allFiles[index].ToString(), values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9], values[10], values[11], values[12], values[13], values[14], values[15], values[16], values[17]);    

//Check if duplcatie
foreach (WaardeObjecten item in listOfWaardeObjecten)
{
      if (item.getIndex() == obj.getIndex())
      {
               listOfWaardeObjecten.Remove(item);
      }
}

//Add to arraylist
listOfWaardeObjecten.Add(obj);

//writedata to log file
writeLogFile();

Code for writing all arraylist elements to .txt file:

//Creating a streamwriter to write to the file with the path of logFileName.
using (FileStream fs = new FileStream(logFileName, FileMode.Truncate, FileAccess.Write))
using (var sw = new StreamWriter(fs))
{
      foreach (WaardeObjecten item in listOfWaardeObjecten)
      {
           sw.WriteLine(item.getTotalString());
      }
}

Now I'm getting the following error:

"An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll."

"Additional Information: The collection has been changed. The inventory operation may not be performed."

UPDATE

The code im using now to remove duplicates:

//Check if duplcatie
for (int i = listOfWaardeObjecten.Count - 1; i >= 0; i--)
{
     if (i == obj.getIndex())
     {
           listOfWaardeObjecten.RemoveAt(i);
     }
}

//Add to arraylist
listOfWaardeObjecten.Add(obj);

//writedata to log file
writeLogFile();

Code im using to write textfile:

//Creating a streamwriter to write to the file with the path of logFileName.
using (FileStream fs = new FileStream(logFileName, FileMode.Truncate, FileAccess.Write))
using (var sw = new StreamWriter(fs))
{
     foreach (WaardeObjecten item in listOfWaardeObjecten)
     {
          sw.WriteLine(item.getTotalString());
     }
}

Output if i first do 10-15 images with all different values and i rewind to the first image and put all values on "N.v.t.":

 2. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm04.JPG : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
 4. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm06.JPG : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
 6. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm08.JPG : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
 8. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\Detail tbv blisemafleiding A-1101.jpg : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
 10. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\IMG-20131108-01010.jpg : Discipline: Preset1 | Soort: Preset1 | Afdruk: Preset1 | Test1: Preset1 | Test2: Preset1 | Test3: Preset1 | Test4: Preset1 | Test5: Preset1 | Test6: Preset1.
 1. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm02.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
  3. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm05.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
  5. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\A 1101 laadarm07.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
  7. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\beschadigde isolatie.jpg : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
  9. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\A-1101-OSBL\99_History\fotos\Foto tbv bliksemafleiding 1.jpg : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
  11. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\C-1106-final\99_History\Inspecties\DSC00864.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
  12. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\C-1107-fnal\99_History\Inspecties\DSC00864.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
  13. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\H-1104-final\99_History\Inspecties\100_0671.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..
  14. C:\Users\Gebruiker\Desktop\MCA JPG2\05_Mechanical\01_Equipment\1100-final\P-1111-final\99_History\Onderhouds rapportages\2014 wk50  div schades\P-1111 eerste schadepomp  2.JPG : Discipline: N.v.t. | Soort: N.v.t. | Afdruk: N.v.t. | Test1: N.v.t. | Test2: N.v.t. | Test3: N.v.t. | Test4: N.v.t. | Test5: N.v.t. | Test6: N.v.t..

As you can see not all the values are updates to "N.v.t.".

Hope anyone can help!

8
  • 1
    You can't modify a collection that you're iterating over with foreach. Try a for loop and iterate backwards. Commented Oct 24, 2017 at 11:18
  • So i have to use a for loop instead of for each? and remove at (i)? How do i call the getIndex() function of that object aswell? Commented Oct 24, 2017 at 11:19
  • Yes, that should work, and start at the end and work backwards with i--. Commented Oct 24, 2017 at 11:20
  • example: listOfWaardeObjecten[2].getIndex() is not working? Commented Oct 24, 2017 at 11:22
  • listOfWaarObjecten[2] returns a WaardeObject, which has no knowledge of it's index. It really shouldn't be necessary, since you already know that the index (i) is 2. Commented Oct 24, 2017 at 11:24

1 Answer 1

5

You can not modify/ remove item from collection when using foreach loop on collections. But if you want to remove item from collection then use for loop instead.

for (int i = listOfWaardeObjecten.Count-1; i >= 0; i--)
{
   if (i == obj.getIndex())
      {
         listOfWaardeObjecten.RemoveAt(i);
      }
}

In case if you don't want to use for loop then, you can use break statement with foreach loop. See here.

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

14 Comments

As stated by Equalsk above, you will need to iterate counting down over the collection, since the index numbers will change after a deletion. So use for (int i = lostOfWaardeObjecten.Count - 1; i >= 0; i--) { ... }
For some reason the code seems fine but its not working in my program yet, can i update the post? or do i need to make a new one? Cant see why it is not working.
I've tried 5 images, and with all 5 images(duplicates) it went into the IF-condition.
It says i can not use .Where in an arraylist.
I can switch it up so list, however i never used list, give me a second.
|

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.