Using Item Property Of Dictionary Class In VB.NET

In this article I explained about Item Property of Dictionary Class.
  • 2453

Item  

The Item property gets and sets the value associated with the specified key. The following code snippet sets and gets an items value.

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)

        ' Read all data

        Console.WriteLine("Student Details List:")

        Console.WriteLine()

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

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

        Next

        Console.WriteLine()

        ' Set Item value

        StudentDetailList("Mahesh") = 20

        ' Get Item value

        Dim age As Int16 = Convert.ToInt16(StudentDetailList("Mahesh"))

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

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

        Next

        Console.WriteLine()

        Console.ReadLine()

    End Sub

End Module

Output:

ItemProperty-of-dictionaryClass.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.