0

I'm having difficulty with a dictionary that I want to compare an updated version to the original version.

The first method passes in the dictionary, then from there it gets passed to a static helper class that updates that dictionary.

Before I pass the original dictionary to the helper class, I want to make a copy of the original dictionary so I can compare.

This is where I'm having trouble. After the helper class, the 'copy' of the dictionary has been updated too.

I've even tried making a struct that contains a dictionary thinking that'd copy the original dictionary values, but that seems to be by ref too! Here is a snippet of the code.

public PartialViewResult updateItem(string submit, FormCollection Collection)
{            
    SurveyItem UpdatedItem = new SurveyItem();
    ItemSettingsCopy OriginalSettings;

    ItemBank CurrentSurvey = (ItemBank)Session["Survey"];

    string _itemName = (string)Session["CurrentItem"];

    OriginalSettings.ItemSettings = CurrentSurvey[_itemName].ItemSettings;  
    //this is where I'm trying to make a copy of the original settings.

    UpdatedItem = BankManagerHelper.UpdateItem(CurrentSurvey[_itemName], Collection, submit);  //static item now updates the fields in the item

    //AT THIS POINT OriginalSettings.ItemSettings HAS BEEN CHANGED TOO
dtb
  • 213,145
  • 36
  • 401
  • 431
Scottingham
  • 906
  • 2
  • 11
  • 26

2 Answers2

0

This is where I'm having trouble. After the helper class, the 'copy' of the dictionary has been updated too.

Yes, as your are actually updating the same variable in memory. When you pass a Dictionary as parameter of a function, you actually pass a reference to your variable.

What you could do is create a new dictionary that contains the same list of objects before you call your UpdateItem method. You'll then have 2 different objects in memory, so you'll be able to compare them.

Note that you might want to create new instances of the items that are stored in your dictionary, or both dictionary will contain references to the same objects (I don't know if you want to compare dictionaries themselves or objects stored into dictionaries).

ken2k
  • 48,145
  • 10
  • 116
  • 176
0

You need to clone the dictionary.

This answer is a way to go. https://stackoverflow.com/a/139841/61256

[This code was copied from the link]

public static Dictionary<TKey, TValue> CloneDictionaryCloningValues<TKey, TValue>
                        (Dictionary<TKey, TValue> original) where TValue : ICloneable
{
    Dictionary<TKey, TValue> ret = new Dictionary<TKey, TValue>(original.Count,
                                                        original.Comparer);
    foreach (KeyValuePair<TKey, TValue> entry in original)
    {
       ret.Add(entry.Key, (TValue) entry.Value.Clone());
    }
    return ret;
}
Community
  • 1
  • 1
Matt
  • 3,664
  • 3
  • 33
  • 39
  • What we're dealing with here is deep-copying vs shallow-copying > http://en.wikipedia.org/wiki/Object_copy – MattDavey Feb 10 '12 at 17:29
  • Thanks! I ended up using this: var newDictionary = oldDictionary.ToDictionary(entry => entry.Key, entry => entry.Value); – Scottingham Feb 10 '12 at 17:35