C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
IntegerStringsBooleanVB.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
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.
StringBuilderVB.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
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 ValuesVB.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