Reputation: 95
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!
Upvotes: 1
Views: 74
Reputation: 5962
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.
Upvotes: 5