mooman_fl Posted May 28, 2004 Posted May 28, 2004 I am trying to make a new class, IndexedHashtable. The idea is that each DictionaryEntry in the hashtable has an index number that you can search by as well as by the usual method (by key name). I can get it to do this rather easy with the following code: Public Class IndexedHashtable Inherits Hashtable Public ReadOnly Property ItemByIndex(ByVal index As Integer) As DictionaryEntry Get Dim _index As Integer = Me.Count - 1 Dim item As DictionaryEntry If index > (Me.Count - 1) Then Throw New IndexOutOfRangeException For Each item In Me If _index = index Then Return item _index += 1 Next End Get End Property End Class The problem is that if I add items to the hashtable then iterate through the index the index numbers don't return the entries in the order they were added. Almost, but not quite. As an example, made a form with a button and add the following code: Dim count As Integer Dim test As IndexedHashtable Dim dict As DictionaryEntry test = New IndexedHashtable test.Add("First", "Uno") test.Add("Second", "Dos") test.Add("Third", "Tres") test.Add("Fourth", "Quatro") test.Add("Fifth", "Sinco") test.Add("Sixth", "Seis") For count = 0 To (test.Count - 1) dict = test.ItemByIndex(count) Console.WriteLine("Key: {0} Value: {1}", dict.Key, dict.Value) Next This produces the following output: Key: Fourth Value: Quatro Key: First Value: Uno Key: Second Value: Dos Key: Third Value: Tres Key: Fifth Value: Sinco Key: Sixth Value: Seis Notice that index 0 is the fourth item that was added... everything else is in order. Is there a reason for this? Am I missing something? And most importantly is there a better, or more efficient way, of getting the results I am looking for? Quote "Programmers are tools for converting caffeine into code." Madcow Inventions -- Software for the Sanity Challenged.
keitsi Posted May 28, 2004 Posted May 28, 2004 I've also noticed this behavior. You could use arraylist to keep the order? I mean like adding the keys to arraylist in the order they were added, then Return Me(arraylist(index)) That takes up a bit more memory though. Quote BS - Beer Specialist
*Experts* Nerseus Posted May 28, 2004 *Experts* Posted May 28, 2004 Use the SortedList class, no need to make your own. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
mooman_fl Posted May 28, 2004 Author Posted May 28, 2004 Thank you Nersus... somehow I had missed that class. That is exactly what I was looking for. Quote "Programmers are tools for converting caffeine into code." Madcow Inventions -- Software for the Sanity Challenged.
Rodenberg Posted May 29, 2004 Posted May 29, 2004 The point of a hashtable is to insert objects into an index derived by their hash value. This means that 2 objects, regardless of type or value, will not necessarily be inserted next to each other. Determining location is what the key is used for ;) Quote
mooman_fl Posted May 29, 2004 Author Posted May 29, 2004 The point of a hashtable is to insert objects into an index derived by their hash value. This means that 2 objects' date=' regardless of type or value, will not necessarily be inserted next to each other. Determining location is what the key is used for ;)[/quote'] I kind of got that impression from the way things worked. Anyway I simply went with a SortedList since I needed the flexibility of both ways of doing things. So far I have no complaints. Quote "Programmers are tools for converting caffeine into code." Madcow Inventions -- Software for the Sanity Challenged.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.