Computer Science(9608) notes and Resources
Implementing
a dictionary using Dictionary class in Visual Basic
Module Module1
Dim
dictionary As New
Dictionary(Of String,
Integer)
Sub
InsertInDictionary(ByVal key As String, ByVal value As Integer)
If
dictionary.ContainsKey(key) Then
Console.WriteLine(" Key alreadly exist! ")
Else
dictionary.Add(key, value)
End If
End Sub
Sub
findInDictionary(ByVal key As String)
If
dictionary(key) Then
Console.WriteLine(" Found and its value is: " &
dictionary(key))
Else
Console.WriteLine(" Not Found ")
End If
End Sub
Sub removeFromDictionary(ByVal key As String)
dictionary.Remove(key)
End Sub
Sub
printValueInDictionary(ByVal key As String)
Console.WriteLine(dictionary(key))
End Sub
Sub
printNoOfEntriesInDictionary()
Console.WriteLine(dictionary.Count)
End Sub
Sub
printDictionary()
Dim
pair As KeyValuePair(Of
String, Integer)
For Each pair In
dictionary
Console.WriteLine("{0}, {1}", pair.Key, pair.Value)
Next
End Sub
Sub Main()
Console.WriteLine(" Inserting 4 items in dictionary")
InsertInDictionary("John", 10)
InsertInDictionary("maria", 90)
InsertInDictionary("sofia", 50)
InsertInDictionary("Mia Kalifa", 69)
Console.WriteLine()
Console.Write("
Value of key sofia: ")
printValueInDictionary("sofia")
Console.WriteLine()
Console.Write("
Number of items in dictionary: ")
printNoOfEntriesInDictionary()
Console.WriteLine()
Console.WriteLine(" Remove one item from dictionary")
removeFromDictionary("Mia Kalifa")
Console.WriteLine()
Console.WriteLine(" Print whole dictionary")
printDictionary()
Console.WriteLine()
Console.WriteLine(" find an item:")
findInDictionary("sofia")
Console.ReadLine()
End Sub
End Module
- CIE A-LEVEL COMPUTER SCIENCE ( 9608 )
- CIE A-LEVEL COMPUTER SCIENCE ( 9608 )
https://drive.google.com/file/d/1nFTB6Rf-T16I5EAVcE5LYFovRS7LgQmv/view?fbclid=IwAR1vVz0cEIFKa9MqBfXs9-d2gPj4UcHuCMfwY9FS4FOGAwXoFa2a7KaDUvM
ReplyDelete