Reputation: 1855
I have the following code.
private void LoopThroughDependsIssues(JIRAOperations jiraOps, string jiraURL, string token, string username, string password, string projectKey, string exportTargetPath, string branch, SVNOperations svnOps, string svnExePath, string changesetDBFile, DependencyManager mgr)
{
var tempVar = mgr.Dependencies;
foreach (var item in tempVar)
{
if (item.depends.Length > 0)
{
var templist = item.depends;
var listissues1 = templist.Split(',');
for (var i = 0; i < listissues1.Length-1; i++)
{
var newissue1 = new string[] { listissues1[i].ToString() };
newissue1.getChangeSet(jiraOps, jiraURL, token, username, password, projectKey, exportTargetPath, branch, svnOps, svnExePath, changesetDBFile, mgr);
}
//throw new Exception("Dependencies found");
}
}
}
In this, i am iterating through the mgr.Dependencis
collection. This value is changing in newissue1.getChangeSet(jiraOps, jiraURL, token, username, password, projectKey, exportTargetPath, branch, svnOps, svnExePath, changesetDBFile, mgr);
So for every calling of this method, my collection value is increasing. But for first time it is working fine. But while iterating for second time it is giving exception as
Collection was modified; enumeration operation may not execute.
I think this exception is coming for changing the collection. How to handle this situation.?
My class definition as follows.
public class Dependency
{
public string issueID { get; set; }
public string jirastatus { get; set; }
public int dependencyFound { get; set; }
public string depends { get; set; }
public string linked_issues { get; set; }
}
public class DependencyManager
{
public List<Dependency> Dependencies { get; private set; }
public DependencyManager()
{
this.Dependencies = new List<Dependency>();
}
}
Upvotes: 1
Views: 3600
Reputation: 3531
You can iterate if you do not use foreach but a regular loop. If you only add rows to the modified collection at the end this will work with minimal changes.
var tempVar = mgr.Dependencies;
var cnt = tempvar.Count(); // Only iterate items available when we start
for (int i = 0; i < cnt; i++)
{
var item = tempVar[i];
Upvotes: -2
Reputation: 8352
If what you want is just to use the collection as it is at the beginning, without caring about the changes, you can just take an snapshot of the collection:
var tempVar = mgr.Dependencies.ToList(); //This will create a new list with the same items
foreach (var item in tempVar) {
...
}
Upvotes: 2
Reputation: 40818
If you index into the Dependencies
collection there wouldn't be an Enumerator created and you can loop through as the collection is modified. This approach could easily cause headaches if the new items are not appended to the end of the list or items are removed.
for(int i = 0; i < mgr.Dependencies.Count; i++)
{
var item = mgr.Dependecies[i];
if (item.depends.Length > 0)
{
// code unchanged
}
}
A safe approach would be to use a Queue
and populate it with the initial items from mgr.Dependencies
and then Enqueue
any additional items you want to process.
var toBeProcessed = new Queue<Dependency>(mgr.Dependencies);
while(toBeProcessed.Count > 0)
{
var item = toBeProcessed.Dequeue();
// loop
// if a new dependency gets added that needs processing, just add it to the queue.
toBeProcessed.Enqueue(newissue1);
}
Upvotes: 5