Dictionary Methods In VB.NET

In this article I explained various dictionary methods in VB.NET.
  • 10607

Dictionary Methods

The Dictionary class is a generic collection and provides all common methods to add, remove, find and replace items in the collection.

Add Items

The Add method adds an item to the Dictionary collection in form of a key and a value. The following code snippet creates a Dictionary and adds an item to it by using the Add method.

Dim StudentDetailList As New Dictionary(Of String, Int16)()
StudentDetailList.Add("Mahesh", 35)    

Alternatively, we can use the Item property. If the key does not exist in the collection, a new item is added. If the same key already exists in the collection, the item value is updated to the new value. The following code snippet adds an item and updates the existing item in the collection.

StudentDetailList("Mahesh") = 20
StudentDetailList("Raj") = 9

Remove Item

The Remove method removes an item with the specified key from the collection. The following code snippet removes an item.

' Remove item with key = 'Mahesh'
StudentDetailList.Remove("Mahesh")
 

The Clear method removes all items from the collection. The following code snippet removes all items by calling the Clear method.  

' Remove all items
StudentDetailList.Clear()
 

Find a Key  

The ContainsKey method checks if a key is already exists in the dictionary. The following code snippet checks if a key is already exits and if not, add one.  

If Not StudentDetailList.ContainsKey("Mahesh") Then
            StudentDetailList("Mahesh") = 20

End If 

Find a Value  

The ContainsValue method checks if a value is already exists in the dictionary. The following code snippet checks if a value is already exits.

If Not StudentDetailList.ContainsValue(9) Then

            Console.WriteLine("Item found")

End If

Here is the complete sample code showing how to use these methods.

Imports System.Collections.Generic

Module Module1

    Sub Main()

        ' Create a dictionary with string key and Int16 value pair

        Dim StudentDetailList As New Dictionary(Of String, Int16)()

        StudentDetailList.Add("Mahesh", 35)

        StudentDetailList.Add("Raj", 25)

        StudentDetailList.Add("Suresh", 29)

        StudentDetailList.Add("Nitin", 21)

        StudentDetailList.Add("Aman", 84)

        ' Count

        Console.WriteLine("Count: {0}", StudentDetailList.Count)

        ' Set Item value

        StudentDetailList("Raj") = 9

        If Not StudentDetailList.ContainsKey("Mahesh") Then

            StudentDetailList("Mahesh") = 20

        End If

        If Not StudentDetailList.ContainsValue(29) Then

            Console.WriteLine()

            Console.WriteLine("Item found")

        End If

        ' Read all items

        Console.WriteLine("All items:")

        Console.WriteLine()

        For Each student As KeyValuePair(Of String, Int16) In StudentDetailList

            Console.WriteLine("Key: {0}, Value: {1}", student.Key, student.Value)

        Next

        ' Remove item with key = 'Aman'

        StudentDetailList.Remove("Aman")

        Console.WriteLine()

        Console.WriteLine("List after removal of item with key='Aman'")

        For Each student As KeyValuePair(Of String, Int16) In StudentDetailList

            Console.WriteLine("Key: {0}, Value: {1}", student.Key, student.Value)

        Next

        ' Remove all items

        StudentDetailList.Clear()

        For Each student As KeyValuePair(Of String, Int16) In StudentDetailList

            Console.WriteLine("Key: {0}, Value: {1}", student.Key, student.Value)

        Next

        Console.WriteLine("All Items removed")

        Console.ReadLine()

    End Sub

End Module

Output:

DictionaryMethods-in-vb.net.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.