Reputation: 7532
I am building a program that is parsing a plain text database file and creating associations so I can export attachments from the system.
Dictionary<string, string> AttachementsDictionary = new Dictionary<string, string>();
Dictionary<string, string> BacklogLookupDictionary = new Dictionary<string, string>();
Dictionary<string, BacklogItem> BacklogItemDictionary = new Dictionary<string, BacklogItem>();
BacklogItem CurrentBacklogItem = new BacklogItem();
Dictionary<string, Product> ProductDictionary = new Dictionary<string, Product>();
Product CurrentProduct = new Product();
string StoryScrumId = "";
Console.WriteLine("Loading Data From ScrumWorks Database");
//You need access to this server to run this locally.
string[] lines = System.IO.File.ReadAllLines(@"\\dxScrum01v\ScrumWorksPro\scrumworks\data\hypersonic\scrumworks.script");
Console.WriteLine("Creating Object Dictionaries");
foreach (string line in lines)
{
if (line.Contains("INSERT INTO ATTACHMENT"))
{
AttachementsDictionary.Add(line.Split('(', ',')[1], line.Split('\'', '\'')[1]);
}
if (line.Contains("INSERT INTO BACKLOGITEM_ATTACHMENT"))
{
if (!BacklogLookupDictionary.ContainsKey(line.Split('(', ',')[1]))
{
BacklogLookupDictionary.Add(line.Split(',', ')')[1], line.Split('(', ',')[1]);
}
}
if (line.Contains("INSERT INTO BACKLOGITEMEJB VALUES"))//-324048562862518297
{
CurrentBacklogItem.ProductScrumId = Regex.Split(line, ",(?=(?:[^']*'[^']*')*[^']*$)")[7];
CurrentBacklogItem.StoryTitle = Regex.Split(line, ",(?=(?:[^']*'[^']*')*[^']*$)")[12];
string test = line.Split('(', ',')[1];
BacklogItemDictionary.Add(line.Split('(', ',')[1], CurrentBacklogItem);
}
if (line.Contains("INSERT INTO PRODUCTEJB VALUES"))
{
CurrentProduct.ProductName = line.Split('\'', '\'')[1].Replace(@"'", string.Empty);
CurrentProduct.StoryPrefix = Regex.Split(line, ",(?=(?:[^']*'[^']*')*[^']*$)")[4].Replace(@"'", string.Empty);
ProductDictionary.Add(line.Split('(', ',')[1], CurrentProduct);
}
}
Above is the code to create dictionaries that I use to deal with the associated relationships in the SQL database.
When it runs it appears as the BacklogItemDictionary is populating correctly:
Each backlog item being added to the dictionary is unique:
However once the foreach loop ends every value changes to the same incorrect one:
Every BacklogItem object is the same as above for every key. What am I doing to make the dictonary behave this way? How do I fix it?
Upvotes: 3
Views: 102
Reputation: 460058
Instead of creating one instance of BacklogItem
before the loop which you then reuse later you should create the BacklogItem
in the loop. Otherwise you are only changing the properties of the single instance on every iteration, actually the last line wins.
So instead of:
BacklogItem CurrentBacklogItem = new BacklogItem();
foreach (string line in lines)
{
.... (changes properties everytime and adds the same instance to the dictionary)
}
this...
foreach (string line in lines)
{
BacklogItem CurrentBacklogItem = new BacklogItem();
.... (initializes this object and adds it to the dictionary)
}
Upvotes: 9