Reputation: 385
I am running into an issue and not really sure what is going on, hopefully someone can help!
I am gathering entries from a database and placing them in a list collection, I am using this list collection to populate a dictionary of "active" things, I am using the list as a template and the "active" entry items are all that will be manipulated until an "active" thing is removed and a new instance of the "active" thing is inserted into the dictionary from the list collection. The issue I am seeing is that the items in the list collection are being updated as well as the dictionary items.
This is a problem for me at at least. I am probably doing something horribly wrong hopefully someone can provide a better solution.
For example:
public class DataEntry
{
public string DataOne { get; set; }
public int DataTwo { get; set; }
}
public static List<DataEntry> dataCollection = new List<DataEntry>();
public static Dictionary<int, DataEntry> ActiveList = new Dictionary<int, DataEntry>();
private static int activeIndex = 0;
public static void LoadListFromDB()
{
dataCollection.Add(new DataEntry() { DataOne = "Lakedoo", DataTwo = 25 });
foreach (DataEntry de in dataCollection)
{
ActiveList.Add(activeIndex++, de);
}
for (int i = 0; i < 5; i++)
{
ActiveList[0].DataTwo -= 2;
}
if (ActiveList[0].DataTwo < 25)
{
ActiveList.Remove(0);
}
foreach (DataEntry de in dataCollection)
{
ActiveList.Add(activeIndex++, de);
}
}
Upvotes: 0
Views: 104
Reputation: 84
As Mitch pointed out, you are storing references to the same object. Essentially, you are creating a bunch of DataEntry
objects and placing them in your List
, but when you build your Dictionary
, you are adding a reference to the same object as is in your List
.
// Create a DataEntry object
DataEntry de = new DataEntry() { DataOne = "Lakedoo", DataTwo = 25 };
// Add it to the List object
dataCollection.Add(de)
// Add it to the Dictionary object
ActiveList.Add(activeIndex++, de);
At this point you have one DataEntry
object, but two references to the object in the List
and the Dictionary
. Because they both point to the same DataEntry
object, if you modify the object stored in either List
or Dictionary
the changes will be immediately reflected in the other.
Also, is there a reason you are not using a constructor in the DataEntry
class?
Upvotes: 0
Reputation: 720
DataEntry is a class, and therefore a reference type. Both your List and the Dictionary are not collections of the data as you are expecting, as much as collections of references to the data in memory - therefore, both collections are "pointing" to the same DataEntry items. When you change the values of a dataEntry item "inside the list", its changing that single instance in memory. When you say "the items in the list collection are being updated as well as the dictionary items", well, there is only one item, just both collections point to it. Maybe search for "Reference types VS Value Types" etc...
Edit: check out this article on Value + Reference types, which also describes Heap vs Stack (a fundamental which might explain it more to you...) http://msdn.microsoft.com/en-us/magazine/cc301717.aspx
Upvotes: 1
Reputation: 35477
Both your ActiveList
and dataCollection
are pointing to the same DataEntry
instances.
You will have to make a Copy
of the dataEntry before inserting it into the ActiveList
. See Writing a copy constructor.
Then do:
foreach (DataEntry de in dataCollection)
{
ActiveList.Add(activeIndex++, new DataEntry(de));
}
Upvotes: 2