Hashtable help

dotnetnube

Member
Joined
Aug 20, 2008
Messages
8
Programming Experience
Beginner
I have the code below:

Dim criteriaList As Hashtable
Dim criteriaElement As DictionaryEntry

criteriaList.Clear()

criteriaList.Add("FileDateTime", "04/02/2009 19:14:35")
criteriaList.Add("BusinessLocation", "Los Angeles")
criteriaList.Add("ReportDate", "04/02/2009")

For Each criteriaElement In criteriaList
'// Code here
Next


Here is my question: When I do a ForEach to go through the criteriaList hashtable, why is the item with "ReportDate" always at position (0)? I would expect it to be at position (2).

When I iterate through the ForEach, I need the items to be in the same order that they were added to the criteriaList Hashtable above.

Thanks in advance!
 
First up, please post in the most appropriate forum for the topic of your thread. The VS forums are for IDE issues, not language issues. Moved.

Secondly, if you're using VS 2005 then you shouldn't really be using a Hashtable at all. You should be using the generic Dictionary class instead, just as you should be using the generic List class instead of an ArrayList.

As for the question, the point of Hastables and Dictionaries is the ability to retrieve a value based on its key and both classes are optimised for that. To provide the functionality you're asking for the classes must be altered such that retrieval by key becomes slower. As such, neither of those classes provide that functionality because it's generally not needed where storage by key is being used.

To get the functionality you want you would have to use an OrderDictionary, which provides retrieval by key or by index. Retrieval by key from an OrderedDictionary is slower than from a Hashtable or Dictionary as a result.

Having said all that, from the looks of your data you shouldn't be using a collection at all. If you're consitently storing that same set of data then you should be declaring a class or structure to represent that set of data specifically, e.g.
VB.NET:
Public Structure SomeType
    Public FileTime As Date
    Public BusinessLocation As String
    Public ReportDate As Date
End Structure
 
First up, please post in the most appropriate forum for the topic of your thread. The VS forums are for IDE issues, not language issues. Moved.

I thought I was in the right spot, but apparently not. I can assure you that it was not intentional.

Secondly, if you're using VS 2005 then you shouldn't really be using a Hashtable at all. You should be using the generic Dictionary class instead, just as you should be using the generic List class instead of an ArrayList.

This was code that I inherited. There was a problem with the code and I was asked to fix it, so the hashtable was not my idea, it was existing code that I was trying to make use of.

Why use the generic List class instead of an ArrayList?
 
Why use the generic
Because they provide a strongly typed interface. The ArrayList/HashTable are typeless (all interface is Object type), where you have to cast all the time to interact with correct type, and where the compiler won't help you ensure the items added are actually of correct type at design time.
 
Back
Top