You are on page 1of 2

data dictionary

Module Module1

Sub Main()

Dim dictionary As New Dictionary(Of String, Integer)

' Add four entries.

dictionary.Add("Dog", 20)

dictionary.Add("Cat", 1)

dictionary.Add("Fish", 10)

dictionary.Add("Bird", -1)

Console.WriteLine(dictionary("Dog"))

Console.WriteLine(dictionary.Count)

If dictionary.ContainsKey("Dog") Then

' Write value of the key.

Dim num As Integer = dictionary.Item("Dog")

Console.WriteLine(num)

End If

' See if this key also exists (it doesn't).

If dictionary.ContainsKey("Fish") Then

Console.WriteLine(False)

End If

Console.ReadLine()

Dim pair As KeyValuePair(Of String, Integer)

For Each pair In dictionary

' Display Key and Value.

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

Next

Console.ReadLine()
Dim list As New List(Of String)(dictionary.Keys)

' Loop over each string.

Dim str As String

For Each str In list

' Print string and also Item(string), which is the value.

Console.WriteLine("{0}, {1}", str, dictionary.Item(str))

Next

Console.ReadLine()

If dictionary.ContainsValue(20) Then

' Prints true.

Console.WriteLine(True)

End If

Console.ReadLine()

dictionary.Remove("Fish")

Console.ReadLine()

For Each pair In dictionary

' Display Key and Value.

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

Next

Console.ReadLine()

End Sub

End Module

You might also like