Dictionary class using VB.NET

A Dictionary class represents a dictionary in C# that is used to represent a collection of keys and values pair of data. This article demonstrates how to use a dictionary in VB.NET.
  • 9746

Dictionary

A Dictionary class represents a dictionary in C# that is used to represent a collection of keys and values pair of data. This article demonstrates how to use a dictionary in VB.NET.

The Dictionary class is a generic class and can store any data types. This class is defined in the System.Collections.Generic namespace. Before you use a Dictionary class in your code, you must import the System.Collections.Generic namespace using the following line.

Imports System.Collections.Generic 

As other collection classes, the Dictionary class has Add, Remove, RemoveAt and other collection methods. The Key and Value property are used to extract a key and a value from an item in a Dictionary.

The following code snippet creates a Dictionary, adds some data to it and reads data back.

Imports System.Collections.Generic
Module
Module1 
      Sub
Main() 
            Dim
AuthorList As New Dictionary(Of String, Int16) 
           AuthorList.Add("Mahesh Chand", 35) 
           AuthorList.Add("Mike Gold", 25) 
           AuthorList.Add("Praveen Kumar", 29) 
           AuthorList.Add("Raj Beniwal", 21) 
           AuthorList.Add("Dinesh Beniwal", 84)

          ' Read all data

          Console.WriteLine("Authors List"
           For
Each author As KeyValuePair(Of String, Int16) In AuthorList       
                     Console
.WriteLine("Key = {0}, Value = {1}",_

                                     author.Key, author.Value)       
                 Next
author       
               Console
.ReadKey() 
       End
Sub

 End Module

Summary

In this article, we learned how to use a dictionary in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.