473,659 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<stri ng, 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 2208
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<stri ng, 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<clas s
(some classes implement ICloneable, but Dictionary<does n't), but you
can easily do it yourself using the generic ICollection<>.C opyTo() method:

Dictionary<stri ng, myClassClone(Di ctionary<string , myClassdict)
{
KeyValuePair<st ring, myClass>[] rgkvp =
new KeyValuePair<st ring, myClass>(dict.C ount);
Dictionary<stri ng, myClassdictNew = new Dictionary<stri ng,
myClass>(dict.C ount);

dict.CopyTo(rgk vp, 0);

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

return dictNew;
}

Hope that helps.

Pete
Aug 29 '07 #2

"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.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<str ing, 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<clas s
(some classes implement ICloneable, but Dictionary<does n't), but you can
easily do it yourself using the generic ICollection<>.C opyTo() method:

Dictionary<stri ng, myClassClone(Di ctionary<string , myClassdict)
{
KeyValuePair<st ring, myClass>[] rgkvp =
new KeyValuePair<st ring, myClass>(dict.C ount);
Dictionary<stri ng, myClassdictNew = new Dictionary<stri ng,
myClass>(dict.C ount);

dict.CopyTo(rgk vp, 0);

foreach (KeyValuePair<s tring, 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<stri ng, myClassClone(Di ctionary<string , myClassdict)
{
Dictionary<stri ng, myClassdictNew = new
Dictionary<stri ng,myClass>();

foreach (KeyValuePair<s tring, 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<int erface 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<d irectly from the
Dictionary<itse lf.

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
4691
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 my databases by copying the data folder to some place? Then if I meet some disaster, could I just copy the backup folder back to recover my databases? Thank you. Zh.y
5
5648
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 "". Reason code: "". We are backing up to a file system on the server. Can't find anything in the logs. We can manually resubmit the job 15 or 30 minutes later and everything works fine. Any ideas on what might be happening? The documentation on...
6
1787
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: not copied! doc3.txt: copied without errors. doc4.txt: copied without errors. but it does not recognize "\n". I had this problem in Visual BASIC ( ~~ two years ago), and I didn't found re-
28
7372
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 "good" in such terms, that this core data structure is changed only by atomic operations, so that the data structure is always consistent regarding the application. Only the change-operations on the dicts and lists itself seem to cause problems...
3
3175
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 application details. I have a query for each of them to copy all the contents of the table into a new table, then delete all the data from the main table. I need to do this becasue once a year the data needs to be removed, ready for putting in new data. I...
3
2230
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 (so user won't be even able to fill it).
0
1324
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 explorer call with some parameters like: C:\Windows\explorer.exe "C:\Windows\Explorer.exe" /separate,/idlist,:49284:4224,C:\Users\devteamkg
0
1634
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 fail? Context: We are using the ActiveDirectory Membership Provider as part of our authentication solution. It is working great most of the time, but every now and then the AD process on the main server fail. With Windows this is not a...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8628
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.