22 Sep 2009 @ 4:27 PM 

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
Posted By: Jeroen De Dauw
Last Edit: 22 Sep 2009 @ 04:33 PM

EmailPermalink
Tags
Tags: , , , , ,
Categories: Programming


 

Responses to this post » (None)

 
Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


 Last 50 Posts
 Back
Change Theme...
  • Users » 4744
  • Posts/Pages » 197
  • Comments » 156
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About me



    No Child Pages.