Using dictionary using VB.NET

This article shows you how use or add objects in dictionary in VB.NET.
  • 15346

The Dictionary class is a generic class which can store any data types. Every program that uses Dictionary in VB.NET will require the Add subroutine. The Add subroutine requires two arguments in its parameter list, the first being the key of the element to add, and the second being the value that key should have. Now the following helps you to understand how to add objects in dictionary in VB.NET. Lets take a look-

EXAMPLE

Option Strict On 
Imports
System.Collections.Generic
Public Module modMain
    Private employees As Dictionary(Of String, Employee)
    Public Sub Main()
        employees = New Dictionary(Of String, Employee)
        employees.Add("First Employee", New Employee("Raju", "1"))
        employees.Add("Second Employee", New Employee("Amit", "1"))
        Console.WriteLine("There are {0} employees now on file. They are:", employees.Count)
        For Each pair As KeyValuePair(Of String, Employee) In Employees
            Console.WriteLine(" {0}: {1}", pair.Key, pair.Value.Name)
        Next
        Console.ReadLine()
    End Sub
End Module
Public Class Employee
    Private empName As String
    Private empID As String
    Public Sub New(ByVal name As String, ByVal ID As String)
        Me.empName = name
        Me.empID = ID
    End Sub
    Public Property Name() As String
        Get
            Return Me.empName
        End Get
        Set(ByVal value As String)
            Me.empName = Value
        End Set
    End Property
    Public Property ID() As String
        Get
            Return Me.empID
        End Get
        Set(ByVal value As String)
            Me.empID = value
        End Set
    End Property

End Class

OUTPUT

1.bmp
 

CONCLUSION

So we can see that the Dictionary collection is extremely powerful, improve the performance of applications and make simpler checking for duplicates.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.