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>();
}
}
If you index into the
Dependenciescollection 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.A safe approach would be to use a
Queueand populate it with the initial items frommgr.Dependenciesand thenEnqueueany additional items you want to process.