Write a hashtable to text file?

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
166
Location
Chicago
Programming Experience
10+
Hi

I am trying to write the contents of a hashtable to a text file. I am getting an InvalidCastException on the For Each itm line. (when casting from a number, the value must be a number less than infinity)


Here is the code I am trying, can anyone help?
VB.NET:
        Using sw As IO.StreamWriter = New IO.StreamWriter("C:\Friends.txt", True)

            Dim itm As KeyValuePair(Of String, String)

            ' Loop through the hashtable adding each item to the text file
            For Each itm In hFriends

                sw.WriteLine(itm.Key & "§" & itm.Value)

            Next

            sw.Flush()
            sw.Close()

        End Using

Thanks!
 
A Hashtable doesn't contain KeyValuePair(Of String, String) objects. A Dictionary(Of String, String) contains KeyValuePair(Of String, String) objects. A Hashtable contains DictionaryEntry objects. If you know that your keys and values are all Strings then why are you using a Hashtable and not a Dictionary in the first place?
 
To be honest, I'm still dipping my toes in .net, and not always sure which form of storage to use in any particular circumstance yet, bet it an arraylist, dictionary, hashtable, collection, array, etc. :)

In this case id do know that it will always contain strings. Would it be better to use a Dictionary instead of a hashtable?
 
You should always use a generic collection if you know what type of objects it will contain. Hashtables and ArrayLists can store any type of object and will return them via Object references, requiring you to cast each time you retrieve.
 
For some reason, I'd swear i read somewhere awhile back that Collections were a hold-over form classic vb, and it is preferred to use dictionaries, hashtables and arraylists in .net. Is this true?

Also, is there any sort of speed difference between for example, a dictionary and a hashtable?
 
The Microsoft.VisualBasic.Collection class is a hold-over from VB6 and you should never use it. That said, the Dictionary and Hashtable classes are both collections, as are the ArrayList, List, BindingList, StringCollection and countless other classes. "Collection" is a generic term. The Collection class is just one implementation of a collection. The System.Collections namespace is full of other implementations.

All generic collections, of which the Dictionary is an example, have an overhead compared to a non-generic equivalent. That said, the strong-typing provided by generic collections is worth the minuscule performance hit.
 
ok, gotcha. Thank you for explaining. I always appreciate when people take the time to explain in detail. :)

Out of curiosity now though, since both a dictionary and hashtable are generic collections, do these 2 also have the same exact overhead, and performance?
 
A Hashtable is NOT a generic collection, which is the point. The System.Collections.Hashtable class has existed from the beginning of .NET to allow you to store values by key. The System.Collections.Generic.Dictionary(Of TKey, TValue) has now been added to do the same job but also allowing you to fix the types of the keys and values, e.g. a Dictionary(Of Integer, String) can only accept Integer keys and String values. A Hashtable is like a Dictionary(Of Object, Object), although with a little less overhead.
 
Back
Top