Thursday, March 8, 2007

Create a Dictionary class in Vb.NET

Public Class DicInteger
Inherits DictionaryBase

Default Public Property Item(ByVal Id As Integer) As Integer
 Get
Return CType(Dictionary(Id), Integer)
End Get

Set(ByVal Value As Integer)
Dictionary(Id) = Value
End Set
End Property


Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property


Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values()
End Get
End Property


Public Function Contains(ByVal Id As Integer) As Boolean
Return Dictionary.Contains(Id)
End Function


Public Sub Add(ByVal Id As Integer, ByVal IntValue As Integer)
If Dictionary.Contains(Id) Then
Dim PrevVal As Integer = Dictionary.Item(Id)
Dictionary.Remove(Id)
IntValue = PrevVal + IntValue
End If
Dictionary.Add(Id, IntValue)
End Sub

End Class

No comments: