
Implementing Dictionary Operations in VB.NET - Retrieval and Search Functions
Learn how to work with dictionaries in VB.NET by retrieving key-value pairs, searching for specific keys and values, and implementing a program to store characters from A to Z. Explore examples of using For Each loops, For loops, and ContainsKey/ContainsValue methods for efficient data management.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Second Lab Information Retrieval
Dictionary <Tkey, TValue> Parameters : TKey - The type of the keys in the dictionary. TValue - The type of the values in the dictionary. e.g. Dim dict As New Dictionary(Of Integer ,String)() Dim dict As New Dictionary(Of Integer, String)() dict.Add(1,"one") dict.Add(2,"two") dict.Add(3,"three ) dict.Add(4,"four")
Retrieve Key-Value pair from Dictionary Using For Each For Each pair As KeyValuePair(Of Integer, String) In dict MsgBox(pair.Key & " - " & pair.Value) Next Using for loop For i As Integer = 0 To dict.Count - 1 Dim element = dict.ElementAt(i) Dim Key = element.Key Dim Value = element.Value ListBox1.Items.Add(Key.ToString() + " " + Value) Next
Search for a Key If dict.ContainsKey(4) = True Then MessageBox.Show( key is exist ) Else MessageBox.Show("Key does not exist") End If Search for a Value If dict.ContainsValue( three ) = True Then MessageBox.Show( value is exist ) Else MessageBox.Show( value not exist") End If
Write a program to implement the following design, the dictionary contain the character from A to z
Public Class Form1 'To define the dictionary Dim dic As New Dictionary(Of Integer, String)() Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Button1.Enabled = True Button2.Enabled = False Button3.Enabled = False Button4.Enabled = False End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Create Dictionary For i As Integer = 65 To 122 Dim ch As Char = Convert.ToChar(i) dic.Add(i, Convert.ToString(ch)) Next Button2.Enabled = True End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'Display Dictionary in listBox For i As Integer = 0 To dic.Count - 1 Dim element = dic.ElementAt(i) Dim Key = element.Key Dim Value = element.Value ListBox1.Items.Add(Key.ToString() + " " + Value.ToString()) Next Button3.Enabled = True Button4.Enabled = True End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ' Search for Specific Value Dim v As String v = InputBox("Enter the character for Search", "inputbox", "enter here", 75, 75) If dic.ContainsValue(v) = True Then MessageBox.Show(v + " Is Founded") Else MessageBox.Show(v + " Not Founded") End If End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click ' Search for Specific Key Dim k As Integer k = InputBox("Enter the key value for Search", "inputbox", "enter here", 75,75) If dic.ContainsKey(k) = True Then MessageBox.Show(k.ToString() + " Is Founded") Else MessageBox.Show(k.ToString() + " Not Founded") End If End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click End End Sub End Class