Generic Dictionary: save and get data : Generic Dictionary « Generics « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Generics » Generic DictionaryScreenshots 
Generic Dictionary: save and get data
Generic Dictionary: save and get data

Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ComponentModel

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 KeyTypeAs 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

           
       
Related examples in the same category
1.Remove value from geneic dictionaryRemove value from geneic dictionary
w__w_w.__j__a__va__2_s_._c_o___m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.