HomeSearch

VB.NET Tuple Examples

Use the Tuple type and its properties Item1, Item2, and Item3 to hold multiple fields of different types.
Tuple. A Tuple groups related pieces of data. With this useful type, we pass multiple values to a function or return multiple values.
Further details. The Tuple type is useful for grouping relating values into a small class. Using tuples tends to reduce program complexity. But they are not always ideal.Class
First example. We create a Tuple by using the VB.NET language's syntax for generic constructs. We must specify each type of the fields in the Tuple at construction time.

Important: We must initialize the tuple's items to their final values. They cannot be changed later.

Here: In this example, we read the 3 fields Item1, Item2, and Item3. We can only read these fields. We cannot set them.

Types: We specify the types Integer, String and Boolean for our tuple's 3 items.

IntegerStringsBoolean
VB.NET program that uses Tuple 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 False
Example 2. How can you use the Tuple type with functions and subroutines in the VB.NET language? We first instantiate the Tuple as we did above.

And: 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 (a reference type, StringBuilder) in a Tuple.

StringBuilder
VB.NET program that uses Tuple in function 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 3 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
Return multiple values. A tuple can be used to return 2 or more values from a Function. We just declare the return type of the Function to be the required Tuple.

And: Then we must return a tuple on each exit point of the Function. We access the return values with properties like Item1 and Item2.

Multiple Return Values
VB.NET program that returns multiple values with Tuple Module Module1 Function GetTwoValues() As Tuple(Of Integer, Integer) ' Return 2 values from this Function. Return New Tuple(Of Integer, Integer)(20, 30) End Function Sub Main() Dim result As Tuple(Of Integer, Integer) = GetTwoValues() Console.WriteLine("RETURN VALUE 1: {0}", result.Item1) Console.WriteLine("RETURN VALUE 2: {0}", result.Item2) End Sub End Module Output RETURN VALUE 1: 20 RETURN VALUE 2: 30
Notes, tuples. A VB.NET class is self-documenting. Field names, and the type name, help us remember our intentions. But a tuple, which lacks those names, may be harder to understand.
Notes, implementation. There are more secrets to the Tuple type, and these are covered in a C# language article on Tuple. It has details about the Tuple implementation.Tuple
A summary. The Tuple type is a clearly useful one in the VB.NET language. It provides a mechanism to create a class with several items in it without a separate type definition.
Final notes. We cannot modify a Tuple already created. The Tuple makes it easier to pass or return multiple values from a function. It fills a useful niche.Function
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls