How to Assign a Variable to a Tuple

RogerSchlueter-7899 1,426 Reputation points
2025-03-22T22:26:57.7666667+00:00

Working with map data, this line of code gets elevation (and other) data:

Dim PointData As ValueTuple = Await GetElevation(NewPoint)

using this function:

Private Async Function GetElevation(pt As Point) As Task(Of (Elevation As Double, Lat As Double, Lng As Double, Resolution As Double))
....
End Function

However, the line in the first code block gives this compile-time error:

Value of type '(Elevation As Double, Lat As Double, Lng As Double, Resolution As Double)' cannot be converted to 'ValueTuple'.

If I set Option Strict OFF and just use

Dim PointData = Await GetElevation(NewPoint)
Debug.WriteLine(PointData)


a test result is

(-3385.211181640625, 9.040110262794867, -87.89062500000004, 610.8129272460938)

which is, in fact, a ValueTuple.

So my question is why does

Dim PointData As ValueTuple = Await GetElevation(NewPoint)

give an compile-time error?

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,822 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 121.3K Reputation points
    2025-03-22T22:57:02.2466667+00:00

    I think that GetElevation does not return a ValueTuple structure, but a generic tuple with four components. Therefore, it is necessary to specify the types:

    Dim PointData As ValueTuple(Of Double, Double, Double, Double) = Await GetElevation(NewPoint)
    

    However, the components are called PointData.Item1, PointData.Item2, etc., which is not convenient. The solution with Dim PointData = Await GetElevation(NewPoint) is more suitable, because it is short and the names are PointData.Elevation, PointData.Lat, etc. The names are inferred by compiler.

    It is also possible to write a long expression:

    Dim PointData As (Elevation As Double, Lat As Double, Lng As Double, Resolution As Double) = Await GetElevation(NewPoint)
    

    The generic ValueType is not inherited from simple ValueType (https://learn.microsoft.com/en-us/dotnet/api/system.valuetuple-4), therefore the conversion is not possible.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.