0

Unhandled exception: enumeration opperator may not execute, how do I resolve this?

So my last issue was that i didnt fill the object array with actual objects yet then i found this: https://stackoverflow.com/questions/12492422/easy-way-to-initialise-array-of-reference-types private Child[] _children = Enumerable .Range(1, 10) .Select(i => new Child()) .ToArray(); I applied the above solution but it gave me the unhandled exception. Any help would be much appreciated, thank you.🌸 https://code.sololearn.com/cQtFXg4Z1tLJ/?ref=app

5th Oct 2023, 7:50 AM
Asriela Aestas
Asriela Aestas - avatar
5 ответов
+ 1
Asriela Aestas line 114 is the issue: tempList.RemoveAt(j); You are iterating over the tempList collection using guest. The enumeration is set up when the loop is created, so when you try to remove the element the enumeration breaks and it can no longer traverse the collection. Instead of removing the elements, you could add a flag to each guest that sets true or false if the guest is still available.
6th Oct 2023, 12:26 AM
The Darkness
The Darkness - avatar
+ 1
Even if it is cloned, the clone is still a collection. When the enumerator is set up, it looks at the collection to determine how many elements it contains. When the element is removed the length of the collection changes but the enumerator does not get updated. Foreach loops are generally only for accessing data and maybe modifying the individual items but not the collection itself. Here is a link to stackoverflow that talks about the issue some more: https://stackoverflow.com/questions/759966/what-is-the-best-way-to-modify-a-list-in-a-foreach-loop
6th Oct 2023, 1:22 AM
The Darkness
The Darkness - avatar
+ 1
Asriela Aestas The error seems to be from your shuffle algorithm. I commented that out and used a different shuffling method instead(I also shortened some long names, and used non-standard c# code layout. they were getting too hard to read on the app in my phone...) I feel you have to refactor the relationship function. There are overlapping and conficting results in the lists. self should be excluded from personlist. There should be no overlap between friend and enemy list There should be no overlap between crush and enemy, too. Shuffle test: https://techiedelight.com/compiler/?39a009 https://code.sololearn.com/cF7pd1OFL088/?ref=app
6th Oct 2023, 11:45 PM
Bob_Li
Bob_Li - avatar
0
I thought: List <Person> tempList = new List <Person> (Person.personList); Was supposed to clone the list, whats the point of cloning a list of the original list is effected by what your doing to the cloned list?
6th Oct 2023, 12:50 AM
Asriela Aestas
Asriela Aestas - avatar
0
Asriela Aestas shuffle and display function for list and array. https://code.sololearn.com/cquPCWnnF0jW/?ref=app
7th Oct 2023, 8:26 AM
Bob_Li
Bob_Li - avatar