VB.NET KeyValuePair Examples

KeyValuePair (Key and Value properties)

You have two pieces of data that you want to store together in your VB.NET program as a single object. The KeyValuePair type is useful as it defines a Structure of any kind of key and any kind of value.

This VB.NET example set shows the KeyValuePair Structure. KeyValuePair has two member fields.

List of KeyValuePairs

In this first example, we create a List that stores element of type KeyValuePair. Notice how the (Of String, Integer) syntax is used to specify that the key of the pair is a String, and the value is an Integer. When creating a KeyValuePair, you must set the key and the value in the constructor. Finally in this example, we loop through all the elements of the list.

Program that creates List of KeyValuePair instances [VB.NET]

Module Module1
    Sub Main()
	' Create List of key-value pairs.
	Dim list As List(Of KeyValuePair(Of String, Integer)) =
	    New List(Of KeyValuePair(Of String, Integer))
	list.Add(New KeyValuePair(Of String, Integer)("dot", 1))
	list.Add(New KeyValuePair(Of String, Integer)("net", 2))
	list.Add(New KeyValuePair(Of String, Integer)("perls", 3))

	' Loop over pairs.
	For Each pair As KeyValuePair(Of String, Integer) In list
	    ' Get key.
	    Dim key As String = pair.Key
	    ' Get value.
	    Dim value As Integer = pair.Value
	    ' Display.
	    Console.WriteLine("{0}, {1}", key, value)
	Next
    End Sub
End Module

Output

dot, 1
net, 2
perls, 3

Return KeyValuePair

Sometimes you may want to return two values at once from a function in your VB.NET program. Although you could use the ByRef modifier, the KeyValuePair can also be returned. In this example, the GetPair Function returns a new instance of the KeyValuePair type that has a specific key and value; both are of type Integer.

Program that returns KeyValuePair from Function [VB.NET]

Module Module1
    Sub Main()
	Dim pair As KeyValuePair(Of Integer, Integer) = GetPair()
	Console.WriteLine(pair.Key)
	Console.WriteLine(pair.Value)
    End Sub

    Function GetPair() As KeyValuePair(Of Integer, Integer)
	' Create new pair.
	Dim pair As KeyValuePair(Of Integer, Integer) = New KeyValuePair(Of Integer, Integer)(5, 8)
	' Return the pair.
	Return pair
    End Function
End Module

Output

5
8

Tuple

Tuple: type name

The KeyValuePair is restricted to a key and a value, but the Tuple type in the VB.NET language can have more items in its memory. If you need three of more fields grouped together, please check out the Tuple type. One drawback of the Tuple type is that is must be allocated on the managed heap as an object instance, while the KeyValuePair is a structure so it can be allocated in the stack memory sometimes.

Tuple Examples

For Each Dictionary

Foreach loop construct

The easiest way to loop over the keys and values in a Dictionary instance is with the For Each loop construct. In this code, you must use the KeyValuePair type as the type of the enumeration variable in the loop. For more detail, please visit the core Dictionary article on this site.

Dictionary Examples

Summary

The VB.NET programming language

The KeyValuePair structure in the VB.NET language is a useful type for storing a key and a value together in a single object in memory. It is a value type, which makes it more efficient in some cases and less efficient in others. Its most common usage is probably in the For Each loop in a Dictionary.

VB.NET Tutorials
.NET