Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Creating a "backup" of a Dictionary

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JamesB - 29 Aug 2007 16:35 GMT
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, myClass> TempDic = 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!
Peter Duniho - 29 Aug 2007 18:18 GMT
> I have a config screen in my app that updates a dictionary<key,value>.
>
[quoted text clipped - 9 lines]
> 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, myClass> Clone(Dictionary<string, myClass> dict)
    {
        KeyValuePair<string, myClass>[] rgkvp =
            new KeyValuePair<string, myClass>(dict.Count);
        Dictionary<string, myClass> dictNew = new Dictionary<string,
myClass>(dict.Count);

        dict.CopyTo(rgkvp, 0);

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

        return dictNew;
    }

Hope that helps.

Pete
JamesB - 30 Aug 2007 10:07 GMT
>> I have a config screen in my app that updates a dictionary<key,value>.
>>
[quoted text clipped - 39 lines]
>
> 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, myClass> Clone(Dictionary<string, myClass> dict)
       {
           Dictionary<string, myClass> dictNew = new
Dictionary<string,myClass>();

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

           return dictNew;
       }
Peter Duniho - 31 Aug 2007 01:37 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.