Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

Posted

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.

BS - Beer Specialist
  • *Experts*
Posted

Use the SortedList class, no need to make your own.

 

-Nerseus

"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
Posted
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 ;)
Posted
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.

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...