Make Dictionary based on your own Object : Dictionary « Data Structure « VB.Net

Make Dictionary based on your own Object

Imports System
Imports System.Collections
Imports System.Collections.Generic

Public Class MainClass
    Shared Sub Main(ByVal args As String())
        Dim dict As New StudentDictionary
        dict.Add("113-11-1111", New Student("A", "a"))
        dict.Add("222-22-2222", New Student("B", "b"))
        dict.Add("333-33-3333", New Student("C", "c"))
        dict.Add("444-44-4444", New Student("D", "d"))

        Dim student As Student = dict.Item("365-76-5476")

    End Sub

End Class
Public Class Student
    Private m_FirstName As String
    Private m_LastName As String
    Public Sub New(ByVal first_name As String, ByVal last_name As String)
        m_FirstName = first_name
        m_LastName = last_name
    End Sub
    Public Overrides Function ToString() As String
        Return m_FirstName & " " & m_LastName
    End Function
End Class

Public Class StudentDictionary
    Inherits System.Collections.DictionaryBase

    ' Add a Dictionary entry.
    Public Sub Add(ByVal new_key As String, ByVal new_student As Student)
        Dictionary.Add(new_key, new_student)
    End Sub

    ' Return an object with the given key.
    Default Public Property Item(ByVal key As String) As Student
            Return DirectCast(Dictionary.Item(key), Student)
        End Get
        Set(ByVal Value As Student)
            Dictionary.Item(key) = Value
        End Set
    End Property

    ' Return a collection containing the Dictionary's keys.
    Public ReadOnly Property Keys() As ICollection
            Return Dictionary.Keys
        End Get
    End Property

    ' Return a collection containing the Dictionary's values.
    Public ReadOnly Property Values() As ICollection
            Return Dictionary.Values
        End Get
    End Property

    ' Return True if the Dictionary contains this Student.
    Public Function Contains(ByVal key As String) As Boolean
        Return Dictionary.Contains(key)
    End Function

    ' Remove this entry.
    Public Sub Remove(ByVal key As String)
    End Sub
End Class


Related examples in the same category

1.Get parent directoryGet parent directory
2.Create directory with drive letter and without drive letterCreate directory with drive letter and without drive letter
3.Delete a DirectoryDelete a Directory
4.Check if a directory does not exist, create a directoryCheck if a directory does not exist, create a directory
5.Get and set current working directoryGet and set current working directory
6.List all files under a directoryList all files under a directory
7.List all Directories under a directoryList all Directories under a directory
8.Your own Generic List Dictionary Your own Generic List Dictionary
9.Add some elements to the dictionary. There are no duplicate keys, but some of the values are duplicates.
10.The Add method throws an exception if the new key is already in the dictionary.
11.The Item property is the default property
12.The default Item property can be used to change the value associated with a key
13.If a key does not exist, setting the default Item property for that key adds a new key/value pair
14.The default Item property throws an exception if the requested key is not in the dictionary
15.TryGetValue can be a more efficient way to retrieve values
16.ContainsKey can be used to test keys before inserting them
17.When you use foreach to enumerate dictionary elements, the elements are retrieved as KeyValuePair objects.
18.To get the values, use the Values property
19.To get the keys alone, use the Keys property
20.Use the Remove method to remove a key/value pair