Community
Showing results for 
Search instead for 
Do you mean 
Reply

collection was modified enumeration operation may not execute

Accepted Solution Solved
Copper Contributor
Posts: 32
Country: Germany
Accepted Solution

collection was modified enumeration operation may not execute

[ Edited ]

 

I writing a plugin where i have to realise  a foreach-loop  like:

 

foreach (Act.Framework.Contacts.Contact contact in ActAppl.ApplicationState.CurrentContactList)
                    {

                              .

                              .
                            if (contact.ValidEmailAddress != null)
                            {

                                       .....

                             }

                    }

 

 

if a second person (license) is deleting a contact (which is in my CurrentContactList) in the database while the foreach-loop is running,

I gain the following error message:

 

"collection was modified enumeration operation may not execute"

 

How can i avoid/supress this problem?


Accepted Solutions
Solution
Accepted by topic author benoit22
‎09-25-2015 03:20 AM
Copper Contributor
Posts: 32
Country: Germany

Re: collection was modified enumeration operation may not execute

Thanks for the suggestion. I think to solve this problem you have to realise a shallow or deep Copy.

 

My solution:

 

List<Act.Shared.Collections.IData> oldList = ACT_app.ActAppl.ApplicationState.CurrentContactList.ToList();

List<Act.Shared.Collections.IData> newList = oldList.GetRange(0, oldList.Count);

                    foreach (Act.Framework.Contacts.Contact contact in newList)
                        {

                                ...

                            if (contact.ValidEmailAddress != null)
                                {

                                    ...

                                   ...

                                 }

                          }

 

View solution in original post


All Replies
Copper Elite Contributor
Posts: 82
Country: Canada

Re: collection was modified enumeration operation may not execute

[ Edited ]

Try to store the current contact list in a variable at the start of you loop and iterate through that rather than using the CurrentContactList propery.

So something like..

 

var lstContacts = ActAppl.ApplicationState.CurrentContactList;

foreach (Contact contact in lstContacts)
{
    ///
}

 

Ahsan Khalid

Chief Technology Officer
Keystroke Quality Computing Inc.
www.keystroke.ca

Solution
Accepted by topic author benoit22
‎09-25-2015 03:20 AM
Copper Contributor
Posts: 32
Country: Germany

Re: collection was modified enumeration operation may not execute

Thanks for the suggestion. I think to solve this problem you have to realise a shallow or deep Copy.

 

My solution:

 

List<Act.Shared.Collections.IData> oldList = ACT_app.ActAppl.ApplicationState.CurrentContactList.ToList();

List<Act.Shared.Collections.IData> newList = oldList.GetRange(0, oldList.Count);

                    foreach (Act.Framework.Contacts.Contact contact in newList)
                        {

                                ...

                            if (contact.ValidEmailAddress != null)
                                {

                                    ...

                                   ...

                                 }

                          }