
In the VB.NET language, you sometimes want to group together several related pieces of data, but creating a separate class for these is cumbersome. To solve this, you can use the Tuple generic type, for passing multiple values to a function or returning multiple values. The Tuple type is useful for grouping relating values into a small class.
These VB programs show the Tuple type. Tuples hold multiple fields of different types.

To start, you can create a Tuple by using the VB.NET language's syntax for generic constructs. You must specify each type of the fields in the Tuple at construction time. Also, you must initialize the items to their final values. In this example, we read the three fields Item1, Item2, and Item3. You can only read these fields; you cannot set them.
Program that uses Tuple [VB.NET, .NET 4.0]
Module Module1
Sub Main()
' Create new tuple instance with three items.
Dim tuple As Tuple(Of Integer, String, Boolean) = _
New Tuple(Of Integer, String, Boolean)(1, "dog", False)
' Test tuple properties.
If (tuple.Item1 = 1) Then
Console.WriteLine(tuple.Item1)
End If
If (tuple.Item2 = "rodent") Then
Console.WriteLine(0)
End If
If (tuple.Item3 = False) Then
Console.WriteLine(tuple.Item3)
End If
End Sub
End Module
Output
1
FalseHow can you use the Tuple type with functions and subroutines in the VB.NET language? First, you can instantiate the Tuple as we did above; the Tuple becomes part of the function signature. Returning a Tuple value can be declared in much the same way, and is not shown here. Also, this example demonstrates the use of another type (StringBuilder) in a Tuple.
Program that uses Tuple in function [VB.NET, .NET 4.0]
Imports System.Text
Module Module1
Sub Main()
' Use this object in the tuple.
Dim builder As StringBuilder = New StringBuilder()
builder.Append("cat")
' Create new tuple instance with three items.
Dim tuple As Tuple(Of String, StringBuilder, Integer) = _
New Tuple(Of String, StringBuilder, Integer)("carrot", builder, 3)
' Pass tuple as parameter.
F(tuple)
End Sub
Sub F(ByRef tuple As Tuple(Of String, StringBuilder, Integer))
' Evaluate the tuple.
Console.WriteLine(tuple.Item1)
Console.WriteLine(tuple.Item2)
Console.WriteLine(tuple.Item3)
End Sub
End Module
Output
carrot
cat
3
There are more secrets to the Tuple type, and these are covered on this site in the C# language article on Tuple. Please scan that article for details about the Tuple implementation, and its conceptual place in software projects.
Tuple Tips
The Tuple type is a clearly useful one in the VB.NET language, and it provides a mechanism to create a class with several items in it without a separate type definition. Making it easier to pass multiple values or return multiple values from a function, the Tuple fills a useful niche in the VB.NET language and .NET Framework.
VB.NET Tutorials