?
Solved

Disordered Hashtable

Posted on 2005-03-09
6
Medium Priority
?
297 Views
Last Modified: 2010-04-23
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 ...
0
Comment
Question by:UnFiRe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13502651
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?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 600 total points
ID: 13502795
From the Help File:
"Hashtable Class
Represents a collection of key-and-value pairs that are organized based on the hash code of the key."

So they are ordered by the hash code of the key.

If you want to preserve the order then use an ArrayList.

Or why not create a class and return that instead:

Public Class XXX
    Public itemNo As Integer
    Public imgSize As String
    Public imgColor As Color
    Public imgshade As Color
End Class

Public Function getProperties(ByVal itemNo As String) As XXX
    Dim X As New XXX
    X.itemNo = itemNo
    X.imgSizeg = imgBox.Width & "x" & imgBox.Height & "px")
    X.imgColor = product.Colour
    X.imgShade = product.Shade
    Return X
End Function
0
 
LVL 1

Author Comment

by:UnFiRe
ID: 13502873
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?
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!

 
LVL 4

Assisted Solution

by:wyoung76
wyoung76 earned 400 total points
ID: 13503042
The way that a hastable works is what you're seeing:  order is NOT preserved (also as alluded to by Idle_Mind).

There is a nice short discussion on hashtables in this column http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/datastructures20_2.asp about two-thirds of the way down the page.

The author of the column does a much better job of describing how a hashtable works than I've ever been able to do.

For your benefit, here is an excerpt from the linked column:

===  QUOTE  ===

Realize that the order with which the items are inserted and the order of the keys in the Keys collection are not necessarily the same. The ordering of the Keys collection is based on the slot the key's item was stored. The slot an item is stored depends on the key's hash value and collision resolution strategy. If you run the above code you can see that the order the items are enumerated doesn't necessarily match with the order with which the items were added to the Hashtable.

===  QUOTE  ===

This goes a long way to describing the behaviour you're seeing, and why the ordering of the inserted data is coming out in a weird way.  It gets a lot more academic if you want more in-depth reasons, but once again if you want to preserve order, use an ArrayList.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13503295
>> 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
0
 
LVL 1

Author Comment

by:UnFiRe
ID: 13506363
Thank you very much guys!
Very informative
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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