By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

Creating a "backup" of a Dictionary

P: n/a
I have a config screen in my app that updates a dictionary<key,value>.

I want to store a copy of this before going into the config screen so if the
user wants to cancel all their changes I can simply set the working copies
to be the backup.

So, in my code, before the config screen is shown, I do this:

Dictionary<string, myClassTempDic = MainDic;

But when the config screen returns from the showdialog, and the "main"
dictionary has been added to, the temp one has also been added to?
How can I create a copy that doesn't change? Bit confused!

Aug 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
JamesB wrote:
I have a config screen in my app that updates a dictionary<key,value>.

I want to store a copy of this before going into the config screen so if
the user wants to cancel all their changes I can simply set the working
copies to be the backup.

So, in my code, before the config screen is shown, I do this:

Dictionary<string, myClassTempDic = MainDic;

But when the config screen returns from the showdialog, and the "main"
dictionary has been added to, the temp one has also been added to?
How can I create a copy that doesn't change? Bit confused!
All that the line you posted does is copy the _reference_ from one
variable to another. The reference refers to the same instance of the
Dictionary<>.

What you need is a method that will create a whole new instance of the
Dictionary<>. I don't believe this is built into the Dictionary<class
(some classes implement ICloneable, but Dictionary<doesn't), but you
can easily do it yourself using the generic ICollection<>.CopyTo() method:

Dictionary<string, myClassClone(Dictionary<string, myClassdict)
{
KeyValuePair<string, myClass>[] rgkvp =
new KeyValuePair<string, myClass>(dict.Count);
Dictionary<string, myClassdictNew = new Dictionary<string,
myClass>(dict.Count);

dict.CopyTo(rgkvp, 0);

foreach (KeyValuePair<string, myClasskvp in rgkvp)
{
dictNew.Add(kvp.Key, kvp.Value);
}

return dictNew;
}

Hope that helps.

Pete
Aug 29 '07 #2

P: n/a

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
JamesB wrote:
>I have a config screen in my app that updates a dictionary<key,value>.

I want to store a copy of this before going into the config screen so if
the user wants to cancel all their changes I can simply set the working
copies to be the backup.

So, in my code, before the config screen is shown, I do this:

Dictionary<string, myClassTempDic = MainDic;

But when the config screen returns from the showdialog, and the "main"
dictionary has been added to, the temp one has also been added to?
How can I create a copy that doesn't change? Bit confused!

All that the line you posted does is copy the _reference_ from one
variable to another. The reference refers to the same instance of the
Dictionary<>.

What you need is a method that will create a whole new instance of the
Dictionary<>. I don't believe this is built into the Dictionary<class
(some classes implement ICloneable, but Dictionary<doesn't), but you can
easily do it yourself using the generic ICollection<>.CopyTo() method:

Dictionary<string, myClassClone(Dictionary<string, myClassdict)
{
KeyValuePair<string, myClass>[] rgkvp =
new KeyValuePair<string, myClass>(dict.Count);
Dictionary<string, myClassdictNew = new Dictionary<string,
myClass>(dict.Count);

dict.CopyTo(rgkvp, 0);

foreach (KeyValuePair<string, myClasskvp in rgkvp)
{
dictNew.Add(kvp.Key, kvp.Value);
}

return dictNew;
}

Hope that helps.

Pete
Thanks Pete,
Your explanation made complete sense however I couldn't get your code sample
working straight off- firstly the (dict.count) in the first line needed to
be [dict.count] to init the array size I guess, but then it still failed as
"dict" doesn't contain a definition for CopyTo.

I had a play around and came up with a cut down version that seems to work
fine though, so many thanks for your help as it got me in the right
direction!

My Clone method:

Dictionary<string, myClassClone(Dictionary<string, myClassdict)
{
Dictionary<string, myClassdictNew = new
Dictionary<string,myClass>();

foreach (KeyValuePair<string, myClasskvp in dict)
{
dictNew.Add(kvp.Key, kvp.Value);
}

return dictNew;
}

Aug 30 '07 #3

P: n/a
JamesB wrote:
Your explanation made complete sense however I couldn't get your code
sample working straight off- firstly the (dict.count) in the first line
needed to be [dict.count] to init the array size I guess, but then it
still failed as "dict" doesn't contain a definition for CopyTo.
Yeah, sorry. That's what happens when the code is just typed in to the
newsreader. No compiler to help me catch little mistakes like that. :)

The paren-versus-brace issue is obvious and easy to fix, as you have.

The CopyTo() method is actually from the ICollection<interface and is
explicitly implemented, so you have to cast to an ICollection<to use
it. However, as you've already discovered, doing the copy is
superfluous since you can just get the KeyValuePairs<directly from the
Dictionary<itself.

Not sure why I thought the CopyTo() was necessary, but your Clone()
method is fine. :)

Pete
Aug 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.