How to deconstruct ValueTuples in Visual Basic .NET?

707 views Asked by At

Let the following C# code :

static (int, string) FunctionThatReturnsTwoValues()
{
    return (123, "hello");
}

public static void Main(string[] args)
{
    (var someInt, var someString) = FunctionThatReturnsTwoValues();
    Console.WriteLine(someInt);
    Console.WriteLine(someString);
    
    var (someInt2, someString2) = FunctionThatReturnsTwoValues();
    Console.WriteLine(someInt2);
    Console.WriteLine(someString2);
}

I would like to find the Visual Basic equivalent of :

(var someInt, var someString) = FunctionThatReturnsTwoValues();

And :

var (someInt2, someString2) = FunctionThatReturnsTwoValues();

Because apparently, the following does not work :

Public Module Module1
        Function FunctionThatReturnsTwoValues() As (Integer, String)
            return (123, "hello")
        End Function
        
        Public Sub Main(string() args)
            (Dim someInt, Dim someString) = FunctionThatReturnsTwoValues()
            Console.WriteLine(someInt)
            Console.WriteLine(someString)
            
            Dim (someInt2, someString2) = FunctionThatReturnsTwoValues()
            Console.WriteLine(someInt2)
            Console.WriteLine(someString2)
        End Sub
End Module

But rather requires the use of an intermediary identifier, like :

Dim result = FunctionThatReturnsTwoValues()
Console.WriteLine(result.Item1)
Console.WriteLine(result.Item2)

But I don't want to use a "result" object variable if possible. Instead I want to directly name my two local variables and infer their type, just like in the C# code.

Thank you.

EDIT : Apprently the language does not allow it yet, as mentionned in Panagiotis Kanavos's comment.

0

There are 0 answers