J
John Davis
Hi All
I was testing my understanding of Hashtables and I thought that if I
override GetHashCode then i could add an object to hashTable like so:
Dim ht As New Hashtable
ht.Add(s1, s1)
ht.Add(s2, s2)
ht.Add(s3, s3)
Where s* are instance of a student class that overrides GetHashCode to
return the StudentID (code at end of post).
I shouldn't add it like:
ht.Add(s1.GetHashCode(), s1)
as the HashTable calls GetHashCode on the Key for me.
This should then store the Hash (The ID returned from GHC) as the Key and
the student as the object. However if you loop through the HashTable with:
Dim de As DictionaryEntry
For Each de In ht
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value)
Next
It shows that the student is stored as both the Key and the Value. Does this
mean that you should just add objects to the HashTable like:
Dim ht As New Hashtable
ht.Add(s1, Nothing)
ht.Add(s2, Nothing)
ht.Add(s3, Nothing)
Seems to me I have misunderstood something here and thw whole point of a
HashTable is to use a key to get to a value, was hoping somebody could clear
this up for me.
Cheers
John
Student Code
Class Student
Private _Name As String
Private _ID As Integer
Private _Age As Integer
Public Sub New(ByVal Name As String, ByVal ID As Integer, ByVal age As
Integer)
_Name = Name
_ID = ID
_Age = age
End Sub
Public Overrides Function ToString() As String
Return String.Format("{0} is {1} years old and is student no: {2}",
_Name, _Age, _ID)
End Function
Public Overrides Function GetHashCode() As Integer
Return _ID
End Function
End Class
I was testing my understanding of Hashtables and I thought that if I
override GetHashCode then i could add an object to hashTable like so:
Dim ht As New Hashtable
ht.Add(s1, s1)
ht.Add(s2, s2)
ht.Add(s3, s3)
Where s* are instance of a student class that overrides GetHashCode to
return the StudentID (code at end of post).
I shouldn't add it like:
ht.Add(s1.GetHashCode(), s1)
as the HashTable calls GetHashCode on the Key for me.
This should then store the Hash (The ID returned from GHC) as the Key and
the student as the object. However if you loop through the HashTable with:
Dim de As DictionaryEntry
For Each de In ht
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value)
Next
It shows that the student is stored as both the Key and the Value. Does this
mean that you should just add objects to the HashTable like:
Dim ht As New Hashtable
ht.Add(s1, Nothing)
ht.Add(s2, Nothing)
ht.Add(s3, Nothing)
Seems to me I have misunderstood something here and thw whole point of a
HashTable is to use a key to get to a value, was hoping somebody could clear
this up for me.
Cheers
John
Student Code
Class Student
Private _Name As String
Private _ID As Integer
Private _Age As Integer
Public Sub New(ByVal Name As String, ByVal ID As Integer, ByVal age As
Integer)
_Name = Name
_ID = ID
_Age = age
End Sub
Public Overrides Function ToString() As String
Return String.Format("{0} is {1} years old and is student no: {2}",
_Name, _Age, _ID)
End Function
Public Overrides Function GetHashCode() As Integer
Return _ID
End Function
End Class