Search code examples
vb.nettuples.net-4.5

Assign values to Tuple with named values in inline function


I'm supporting a solution using VS 2022 and Framework 4.5.2.

I'm trying to create a tuple with named values inside an inline function and pass it to another function.

I know how to declare the tuple inline without named parameters and assign values, and I know how to declare it inline with named values but not assign the values.

How do I do both?

The following is ok:

Dim q1 =
    myEnumerable.Select(
        Function(x) New Tuple(Of String, String, String)(x.Name, x.URL, x.Name)
    )

But this is not: (I get red squigglies under the values)

Dim q2 =
    myEnumerable.Select(
        Function(x) New Tuple(Of (name As String, link As String, tooltip As String))(x.Name, x.URL, x.Name)
    )

How do I do this?


Solution

  • This syntax will work in one line to produce the named tuple you want

    Dim q2 = myEnumerable.Select(Function(x) (Name:=x.Name, URL:=x.URL, tooltip:=x.Name))
    

    Or using inferred names for the first 2

    Dim q2 = myEnumerable.Select(Function(x) (x.Name, x.URL, tooltip:=x.Name))