Link to home
Start Free TrialLog in
Avatar of UnFiRe
UnFiRe

asked on

Disordered Hashtable

I have a function that returns a hashtable

            Public Function getProperties(ByVal itemNo As String) As Hashtable
                Dim hash As New Hashtable
                hash.Add("itemNo", itemNo)
                hash.Add("imgSize", imgBox.Width & "x" & imgBox.Height & "px")
                hash.Add("color", product.Colour)
                hash.Add("shade", product.Shade)
                Return hash
            End Function


Then i add the keys and values to listbox items:

                Dim enumer As IDictionaryEnumerator = properties.GetEnumerator
                While enumer.MoveNext
                    If enumer.Key = "itemNo" Then
                        lstBox.Items.Add(enumer.Value)

                    Else
                        lstBox.Items.Add(enumer.Key & ": " & enumer.Value)
                    End If
                End While

For some reason i endup having listbox items going in a weird order:

Shade
Color
ItemNo
imgSize

Why would that happen???

I even quickwatched the hash in the Return Hash string and the order was weird
imgSize
itemNo
Shade
Color

When i use enumerator, the order gets even more screwed up ...
Avatar of S-Twilley
S-Twilley

At a guess.. if these items are coming out as a different order to what you added them i.e. itemNo, imgSize, color then shade... it means they're probably being sorted by Value rather than Key...  which order do you want them sorted by? Key I take it?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UnFiRe

ASKER

I add them in exact same order as i show it in the code. So, i suppose, they should be read in exact same order by the enumerator?
And if it is not so, if the keys are sorted somehow by the key, the sorting does not make sence to me.
Shade > Color > ItemNo > imgSize ? (these are the keys)

Idle_Mind, what is meant by hash code of the key?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Idle_Mind, what is meant by hash code of the key?

Some understandable discussion on the topic here:
http://searchdatabase.techtarget.com/sDefinition/0,,sid13_gci212230,00.html
Avatar of UnFiRe

ASKER

Thank you very much guys!
Very informative