Visual Basic ValueTuple

In c#, ValueTuple is same as Tuple to store the sequence of elements of different data types but the only difference is ValueTuples are the value types (structures) rather than reference types (classes) like Tuple and it will overcome the limitations of Tuple class.

 

The ValueTuple has been introduced in .NET Framework 4.7 and it will available in your project if you are using .NET Framework 4.7 or higher, or .NET Standard 2.0 or higher.

 

In case, if you don’t see the ValueType in your project, then you need to install it from the NuGet package manager, for that right-click on your project à select Manage NuGet Packages. In NuGet Package Manager window click on the Browse tab, search for System.ValueTuple and install it like as shown below.

 

Add ValueTuple Package Reference in Visual Basic

Visual Basic Create ValueTuple

In visual basic, we can create and initialize the ValueTuple by specifying values within the parentheses () like as shown below.

 

Dim user = (1, "Rohini", "Guntur", 90.5)

 

Or

 

Dim user1 As ValueTuple(Of Integer, String, String, Double) = (1, "Rohini", "Guntur", 90.5)

 

Or

 

Dim user2 As (Integer, String, String, Double) = (1, "Rohini", "Guntur", 90.5)

If you observe the above ValueTuple declarations, we created and initialized the values in different ways based on our requirements.

 

As discussed, the Tuple object will directly support only 8 elements, but the ValueTuple will support more than 8 elements.

 

Dim numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

Visual Basic Access ValueTuple Elements

In visual basic, we can access the ValueTuple elements same like Tuple object by using Item<positionNumber> properties. For example, the first element can be accessed like Item1, the second element can be accessed like Item2, and so on based on our requirements.

 

Following is the example of accessing the ValueTuple object elements in visual basic.

 

Dim user = Tuple.Create(1, "Rohini", "Guntur", 90.5)

Dim Id = user.Item1 ' 1

Dim name = user.Item2 ' Rohini

Dim location = user.Item3 ' Guntur

In visual basic, we can create a Tuple object even with 1 element using Tuple.Create() method, but to create ValueTuple object it required at least 2 values.

 

' Not a Tuple

Dim number = (1)

' valid ValueTuple

Dim numbers = (1, 2)

If you observe the above code, we created a number object with 1 element. If we try to access the number object element, it won’t show that element (Item1) in the properties.

 

Visual Basic ValueTuple with Single Element

 

In case, if we try to access numbers object elements, it will show the numbers object elements in the properties list like as shown below.

 

Visual Basic ValueTuple Access Elements

Visual Basic ValueTuple Example

Following is the example of creating and accessing the ValueTuple object values in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        Dim user = (1, "Rohini", "Guntur", 90.5)

        Console.WriteLine("*****User Details*****")

        Console.WriteLine("Id:{0}", user.Item1)

        Console.WriteLine("Name:{0}", user.Item2)

        Console.WriteLine("Location:{0}", user.Item3)

        Console.WriteLine("Percentage:{0}", user.Item4)

 

        Dim numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

        Console.WriteLine("*****Numbers*****")

        Console.WriteLine("{0}", numbers.Item1)

        Console.WriteLine("{0}", numbers.Item10)

        Console.WriteLine("{0}", numbers.Item12)

        Console.ReadLine()

    End Sub

End Module

When we execute the above visual basic program, we will get the result as shown below.

 

*****User Details*****

Id:1

Name:Rohini

Location:Guntur

Percentage:90.5

 

*****Numbers*****

1

10

12

Visual Basic Named & Unnamed Tuples

Generally, if we create a Tuple without providing any alternative names for elements like as shown below, then those will be called as unnamed tuples.

 

Dim user = (1, "Rohini", "Guntur", 90.5)

The above tuple object (user) elements can be accessed with default property names like Item1, Item2, and so on because we didn’t provided any name for elements.

 

If we create a Tuple by assigning the names to elements, then that will be called as named Tuple.

 

In visual basic, only ValueTuple objects will allow us to provide the names for elements. Following is the example of creating ValueTuple objects with property names.

 

Dim user = (Id: 1, Name: "Rohini", Location: "Guntur", Percentage: 90.5)

The above ValueTuple object elements can be accessed either by using the specified names or with the default property names (Item1, Item2, and so on) like as shown below.

 

Module Module1

    Sub Main(ByVal args As String())

        Dim user = (Id:=1, Name:="Rohini", Location:="Guntur", Percentage:=90.5)

        Console.WriteLine("*****User Details*****")

        Console.WriteLine("Id:{0}", user.Id)

        Console.WriteLine("Name:{0}", user.Name)

        Console.WriteLine("Location:{0}", user.Location)

        Console.WriteLine("Percentage:{0}", user.Percentage)

        Dim user1 = (Id:=2, Name:="Suresh", Location:="Hyderabad", Percentage:=80.0)

        Console.WriteLine("*****User Details*****")

        Console.WriteLine("Id:{0}", user1.Item1)

        Console.WriteLine("Name:{0}", user1.Item2)

        Console.WriteLine("Location:{0}", user1.Item3)

        Console.WriteLine("Percentage:{0}", user1.Item4)

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created ValueTuple objects (user & user1) and accessing the elements by using names (Id, Name, Location, and Percentage) and with default property names (Item1, Item2, etc.).

 

When we execute the above program, we will get the result as shown below.

 

*****User Details*****

Id:1

Name:Rohini

Location:Guntur

Percentage:90.5

 

*****User Details*****

Id:2

Name:Suresh

Location:Hyderabad

Percentage:80

Visual Basic ValueTuple as Method Parameter

In visual basic, we can use ValueTuple as a method parameter to send multiple values to the method with a single parameter.

 

Following is the example of using ValueTuple as a method parameter in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        Dim user = (1, "Rohini", "Guntur", 90.5)

        GetUserDetails(user)

        Console.ReadLine()

    End Sub

    Public Sub GetUserDetails(ByVal user As (Integer, String, String, Double))

        Console.WriteLine("*****User Details*****")

        Console.WriteLine("Id:{0}", user.Item1)

        Console.WriteLine("Name:{0}", user.Item2)

        Console.WriteLine("Location:{0}", user.Item3)

        Console.WriteLine("Percentage:{0}", user.Item4)

    End Sub

End Module

If you observe the above example, we are sending the ValueTuple object (user) as a parameter to the GetUserDetails() method.

 

When we execute the above program, we will get the result as shown below.

 

*****User Details*****

Id:1

Name:Rohini

Location:Guntur

Percentage:90.5

By using ValueTuple, we can send multiple values as a single parameter to the method based on our requirements.

Visual Basic ValueTuple as Return Type

In visual basic, we can use ValueTuple as a method return type to return the multiple values without using ref or out parameters.

 

Following is the example of using ValueTuple as the return type of method to return the multiple values in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        Dim user = GetUserDetails()

        Console.WriteLine("*****User Details*****")

        Console.WriteLine("Id:{0}", user.Item1)

        Console.WriteLine("Name:{0}", user.Item2)

        Console.WriteLine("Location:{0}", user.Item3)

        Console.WriteLine("Percentage:{0}", user.Item4)

        Console.ReadLine()

    End Sub

    Public Function GetUserDetails() As (Integer, String, String, Double)

        Dim user = (1, "Rohini", "Guntur", 90.5)

        Return user

    End Function

End Module

If you observe the above example, we created a method GetUserDetails() with return type as ValueTuple object to return multiple values.

 

When we execute the above program, we will get the result as shown below.

 

*****User Details*****

Id:1

Name:Rohini

Location:Guntur

Percentage:90.5

This is how we can use the ValueTuple in our applications based on our requirements.