Dictionary in vb.net

In VB.NET, the Dictionary(Of TKey, TValue) class is a collection that stores key-value pairs, where each key is unique. It provides an efficient way to store and retrieve data based on a specific key. The Dictionary class is part of the System.Collections.Generic namespace.

Important functions in VB.Net List

Creating a Dictionary

To create a dictionary, you need to specify the data types of the key (TKey) and the value (TValue).

Dim dict As New Dictionary(Of String, Integer)()

In this example, we create a dictionary that uses String as the key type and Integer as the value type.

Adding Key-Value Pairs

You can add key-value pairs to the dictionary using the Add method.

dict.Add("Apple", 10) dict.Add("Banana", 20) dict.Add("Orange", 30)

In this example, we add three key-value pairs to the dictionary.

Accessing Values by Key

You can retrieve the value associated with a specific key using the indexer (Item) property.

Dim value As Integer = dict("Banana")

In this example, we retrieve the value associated with the key "Banana".

Modifying Values

You can modify the value associated with a specific key by assigning a new value to it using the indexer property.

dict("Apple") = 15

In this example, we change the value associated with the key "Apple" to 15.

Removing Key-Value Pairs

You can remove a key-value pair from the dictionary using the Remove method.

dict.Remove("Orange")

In this example, we remove the key-value pair with the key "Orange" from the dictionary.

Checking if a Key Exists

You can check if a specific key exists in the dictionary using the ContainsKey method. It returns a Boolean value indicating the presence of the key.

If dict.ContainsKey("Apple") Then ' Key exists End If

In this example, we check if the key "Apple" exists in the dictionary.

Iterating through the Dictionary

You can iterate through the key-value pairs in the dictionary using a For Each loop.

For Each kvp As KeyValuePair(Of String, Integer) In dict Dim key As String = kvp.Key Dim value As Integer = kvp.Value ' Perform operations with key and value Next

In this example, we iterate through each key-value pair in the dictionary and access the key and value.

The provided VB.NET source code showcases a selection of frequently utilized functions.

Full Source VB.NET
Imports System.Collections.Generic Imports System.Windows.Forms Module DictionaryExample Sub Main() Dim dict As New Dictionary(Of String, Integer)() ' Adding Key-Value Pairs dict.Add("Apple", 10) dict.Add("Banana", 20) dict.Add("Orange", 30) ' Accessing Values by Key Dim bananaValue As Integer = dict("Banana") ' Modifying Values dict("Apple") = 15 ' Removing Key-Value Pairs dict.Remove("Orange") ' Checking if a Key Exists If dict.ContainsKey("Apple") Then MessageBox.Show("Key 'Apple' exists.", "Key Existence") End If ' Iterating through the Dictionary Dim message As String = "" For Each kvp As KeyValuePair(Of String, Integer) In dict Dim key As String = kvp.Key Dim value As Integer = kvp.Value message += $"Key: {key}, Value: {value}" & vbCrLf Next MessageBox.Show(message, "Iterating through the Dictionary") End Sub End Module

Conclusion

The Dictionary class provides an efficient way to store, retrieve, and manipulate key-value pairs in VB.NET. It is commonly used when you need fast lookup and retrieval of data based on unique keys.