Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Mon Jul 01, 2019 2:33 pm

Hi.

I am trying to consecutively work on ShapeCollection, but it throws an exception "System.InvalidOperationException: Collection was modified; enumeration operation may not execute."
It is on Spire.Presentation 4.6.5. Did you make ShapeCollection to be an immutable collection since 4.3.9 ver?

Before it is thrown, I remove some shapes from this collection, and change font sizes on some. On 4.3.9 it works fine.

AdamShakhabov
 
Posts: 40
Joined: Wed Sep 26, 2018 12:03 pm

Tue Jul 02, 2019 5:46 am

Dear Adam,

Thanks for your inquiry.
Are you removing shapes in a foreach loop? To help us look into your issue accurately, please provide your testing code as well as your input power point file. You could send your file to us via email ([email protected]). Thanks in advance.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1195
Joined: Tue Sep 27, 2016 1:06 am

Tue Jul 02, 2019 6:17 am

Yes, i am using foreach loop. And after removing shapes using a foreach loop, next usage of foreach loop on this collection throws this exception.

AdamShakhabov
 
Posts: 40
Joined: Wed Sep 26, 2018 12:03 pm

Tue Jul 02, 2019 6:51 am

Hello,

Thanks for your prompt reply.
Please note that the foreach is designed to iterate over the collection, it is specified that each item traversed is read-only. It means you can read each item, but cannot modify it.That's why when you remove shape in ShapeCollection, the exception occurred. To avoid the issue, please use the for-loop instead of foreach-loop. If there is still any question, please provide your testing code for a better investigation.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1195
Joined: Tue Sep 27, 2016 1:06 am

Tue Jul 02, 2019 4:50 pm

Hello, thanks for answer :)

But still, the question is not solved because I do not change the code and it works on 4.3.9, but does not on 4.6.5.
Here will be a sample of what I am doing.
Code: Select all
//slide is a slide of some presentation
ShapeCollection shapes = slide.Shapes;

var condShapes = from s in shapes
where s.Width < 200
select s;

foreach(var item in condShapes)
{
shapes.Remove(item);
}

//and then right after that

foreach(var shape in shapes)//here exception is being thrown
{
//some code
}

AdamShakhabov
 
Posts: 40
Joined: Wed Sep 26, 2018 12:03 pm

Wed Jul 03, 2019 5:58 am

Hello,

Thanks for your feedback.
I simulated a PPT file and tested your sample code, but I didn't reproduce your issue. To help further investigate your issue, please share us with your input testing PPT file. Don’t worry, we promise to keep your documents confidential and won’t use them for any other purpose, you could send it to us via email ([email protected]). Thanks in advance.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1271
Joined: Wed Apr 25, 2018 3:20 am

Wed Jul 03, 2019 1:05 pm

Got it, thank you.
I will refactor my code and if anything will appear, I will notify you.

AdamShakhabov
 
Posts: 40
Joined: Wed Sep 26, 2018 12:03 pm

Thu Jul 04, 2019 1:23 am

Hello,

Ok, if you still encounter issue, please share us with your testing PPT file to help further investigate it. Hope everything goes well.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1271
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Presentation