Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do you save a Visual Basic dictionary to a file?

Posted on 2013-10-31
8
Medium Priority
?
1,483 Views
Last Modified: 2013-11-02
How does one save a Visual Basic dictionary as a file?

I tried the "tostring" method to convert the dictionary to a string.  That allowed me to save the dictionary to a file, but I do not know how to get it back as there is no "todictionary" method which works with strings as I can tell.

Any thoughts?
0
Comment
Question by:schlaff
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39615785
In the Dictionary what are the data types of the Key and Value entries?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39615788
What is being stored in the dictionary?...what type is the key?...what type is the value?
0
 

Author Comment

by:schlaff
ID: 39615815
I am working with three dictionaries -- a dictionary with string keys and string values, a dictionary with string keys and dictionaries as values and a dictionary with string keys and a class as values.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:schlaff
ID: 39615820
I know that I could write a routine to save the class keys and values separately and retrieve them in the same fashion.  I was hoping that VB.net provided for a more streamlined method.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39616101
The simplest way it to serialize it. You can use either a BinaryFormatter object to have a compact file, or a SoapFormatter to get an XML file.

In order to do that however, all the objects in the collection must be from classes that have been marked with the SerializableAttribute. All the collections and most of the classes in the framework have been built to be serialized, but if you use a custom class, it will need to have the SerializableAttribute in order to work with the formatters.

Otherwise, you end up doing as we did in the old days: loop through the collection, extract the properties of each of the objects, and write them to a file. Do the reverse to recreate the collection,

The formatters will do that for you with 3 lines of code to read and 3 lines of code to write (disregarding the Try...Catch that should usually always be there when handing files). Look at the documentation for the BinaryFormatter, they have a clear example with a HashTable.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39618197
One correction to JamesBurger's answer:  You cannot serialize generic types (which a Dictionary is) via the SoapFormatter. On top of that, it has been deprecated, so it could possibly be removed in an update to the Framework (but it hasn't happened yet, so who knows). You should be able to use the BinaryFormatter without incident.
0
 
LVL 40
ID: 39618833
The SoapFormatter was deprecated in .NET 2.0 in 2005. But it is still living 8 years later, and in framework 4.5, we do not get any warning that it is deprecated when using it, nor is the documentation stating that it is.

I thus suspect that they dropped their decision of eventually scrapping it. I cannot find any official reference from Microsoft that states that it is deprecated anymore. However, because old stuff does not die easily on the Internet, you still see programmers everywhere repeating that it is deprecated.

I use the BinaryFormatter for most of my work and was not aware that the SoapFormatter was having problems with generics. Thanks for pointing that out kaufmed.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39619228
From http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter.aspx:

Beginning with the .NET Framework 2.0, this class is obsolete. Use BinaryFormatter instead.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question