Class TypeHashTable

From the archive: written in 2009 and kept for the record. Much of it is out of date.

While working on the Download class of the .Net Download Library yesterday, I came across the need to add and retrieve items from a HashTable of a fixed type. After asking at MSDN forums, someone suggested creating for example a StringHashTable, which then contains a number of Shadow methods that ctype a parameter of the type String to Object, and then pass it along to the corresponding native method of the base class. I extended a little on this by creating a general type defined HashTable extension class: TypeHashTable. This class is pretty identical to the String variant, but I added one variable type (see the code below), making it usable for whatever type you need it, without any manual converting. I’m probably not implementing this in the .Net Download library, since the conversion there only requires a one line function, and the philosophy is to keep it as simple and small as possible. Anyway, I’ve put this class on my forums so that anyone can use it. It’s also a neat example of the usage of variable type definitions IMHO 🙂

Public Class TypeHashTable(Of ItemType)
    Inherits Hashtable

    Public Shadows Sub Add(ByVal key As ItemType, ByVal value As ItemType)
        MyBase.Add(CType(key, System.Object), CType(value, System.Object))
    End Sub

    Public Shadows Function Contains(ByVal key As ItemType) As System.Boolean
        Return MyBase.Contains(CType(key, System.Object))
    End Function

    Public Shadows Function ContainsKey(ByVal key As ItemType) As System.Boolean
        Return MyBase.ContainsKey(CType(key, System.Object))
    End Function

    Public Shadows Function ContainsValue(ByVal value As ItemType) As System.Boolean
        Return MyBase.ContainsValue(CType(value, System.Object))
    End Function

    Public Shadows Sub Remove(ByVal key As ItemType)
        MyBase.Remove(CType(key, System.Object))
    End Sub
End Class

Jeroen De Dauw is founder and CEO of Professional Wiki and builds NeoWiki. More about me. Reply by email or on Mastodon, Bluesky or X.