VB.Net/Generics/Generic Dictionary
Generic Dictionary: save and get data
Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ruponentModel
Public Class MainClass
Shared Sub Main()
Dim pair_dictionary As New PairDictionary(Of Integer, String, String)
pair_dictionary.Add(10, "A", "A")
pair_dictionary.Add(32, "B", "B")
pair_dictionary.Add(17, "C", "C")
" Print the values for index 32.
Dim value1 As String = ""
Dim value2 As String = ""
pair_dictionary.GetItem(32, value1, value2)
Console.WriteLine(value1 & ", " & value2)
End Sub
End Class
Public Class PairDictionary(Of KeyType, DataType1, DataType2)
Private Structure DataPair
Public Data1 As DataType1
Public Data2 As DataType2
Public Sub New(ByVal data_value1 As DataType1, ByVal data_value2 As DataType2)
Data1 = data_value1
Data2 = data_value2
End Sub
End Structure
Private m_Dictionary As New Dictionary(Of KeyType, DataPair)
Public ReadOnly Property Count() As Integer
Get
Return m_Dictionary.Count
End Get
End Property
Public Sub Add(ByVal key As KeyType, ByVal data_value1 As DataType1, ByVal data_value2 As DataType2)
m_Dictionary.Add(key, New DataPair(data_value1, data_value2))
End Sub
Public Sub Clear()
m_Dictionary.Clear()
End Sub
Public Function ContainsKey(ByVal key As KeyType) As Boolean
Return m_Dictionary.ContainsKey(key)
End Function
" Return a data pair.
Public Sub GetItem(ByVal key As KeyType, ByRef data_value1 As DataType1, ByRef data_value2 As DataType2)
Dim data_pair As DataPair = m_Dictionary.Item(key)
data_value1 = data_pair.Data1
data_value2 = data_pair.Data2
End Sub
" Set a data pair.
Public Sub SetItem(ByVal key As KeyType, ByVal data_value1 As DataType1, ByVal data_value2 As DataType2)
m_Dictionary.Item(key) = _
New DataPair(data_value1, data_value2)
End Sub
" Return a collection containing the keys.
Public ReadOnly Property Keys() As System.Collections.ICollection
Get
Return m_Dictionary.Keys()
End Get
End Property
" Remove a particular entry.
Public Sub Remove(ByVal key As KeyType)
m_Dictionary.Remove(key)
End Sub
End Class
Remove value from geneic dictionary
Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ruponentModel
Public Class MainClass
Shared Sub Main()
Dim pair_dictionary As New PairDictionary(Of Integer, String, String)
pair_dictionary.Add(10, "A", "A")
pair_dictionary.Add(32, "B", "B")
pair_dictionary.Add(17, "C", "C")
pair_dictionary.Remove(32)
" Print the values for index 32.
Dim value1 As String = ""
Dim value2 As String = ""
Try
pair_dictionary.GetItem(32, value1, value2)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.WriteLine("Value:" & value1 & ", " & value2)
End Sub
End Class
Public Class PairDictionary(Of KeyType, DataType1, DataType2)
Private Structure DataPair
Public Data1 As DataType1
Public Data2 As DataType2
Public Sub New(ByVal data_value1 As DataType1, ByVal data_value2 As DataType2)
Data1 = data_value1
Data2 = data_value2
End Sub
End Structure
Private m_Dictionary As New Dictionary(Of KeyType, DataPair)
Public ReadOnly Property Count() As Integer
Get
Return m_Dictionary.Count
End Get
End Property
Public Sub Add(ByVal key As KeyType, ByVal data_value1 As DataType1, ByVal data_value2 As DataType2)
m_Dictionary.Add(key, New DataPair(data_value1, data_value2))
End Sub
Public Sub Clear()
m_Dictionary.Clear()
End Sub
Public Function ContainsKey(ByVal key As KeyType) As Boolean
Return m_Dictionary.ContainsKey(key)
End Function
" Return a data pair.
Public Sub GetItem(ByVal key As KeyType, ByRef data_value1 As DataType1, ByRef data_value2 As DataType2)
Dim data_pair As DataPair = m_Dictionary.Item(key)
data_value1 = data_pair.Data1
data_value2 = data_pair.Data2
End Sub
" Set a data pair.
Public Sub SetItem(ByVal key As KeyType, ByVal data_value1 As DataType1, ByVal data_value2 As DataType2)
m_Dictionary.Item(key) = _
New DataPair(data_value1, data_value2)
End Sub
" Return a collection containing the keys.
Public ReadOnly Property Keys() As System.Collections.ICollection
Get
Return m_Dictionary.Keys()
End Get
End Property
" Remove a particular entry.
Public Sub Remove(ByVal key As KeyType)
m_Dictionary.Remove(key)
End Sub
End Class