473,387 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Creating a "backup" of a Dictionary

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
3 2190
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: 21novembre | last post by:
Hi all, I made a question several days before to describe my strange trouble of mysqldump. But I still can't figour it out. Well, I just want to ask another question whether I could just backup...
5
by: apple | last post by:
UDBV8 fp 6a - AIX 5.1 We have scheduled cron jobs to do backups. Periodically and starting to occur more frequently, a backup fails with this error: SQL2072N Unable to bind the shared library...
6
by: NJSG | last post by:
I'm creating a Dialog-Based Win32 file backup utility for use with other application. The problem is that I want to get the following in a textbox: doc1.txt: copied without errors. doc2.txt:...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
3
by: Robertf987 | last post by:
Well, I think I've described what I want to do in the title here. In the database, I have two main tables that contain the main data for the database. One for group expenditures, another for...
3
by: Alex.Sh | last post by:
How can i do something like this: i have a form with A percentage : ______ B percentage: ______ C percentage: ______ I want that the sum of these 3 fields *will never* be more than 100...
0
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, well i had no need for it until today, so how can i bring up the "Backup and Restore" Dialog in Vista? In W2K and XP its still ntbackup.exe but with vista it changed to something like a...
0
by: myth0s | last post by:
Hi, The question: Is it possible to have two differents ActiveDirectory Membership Provider in web.config and change at run-time from the default provider to the second provider if the first one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.