3

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.

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
Virus721
  • 8,061
  • 12
  • 67
  • 123
  • 2
    Read [this](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/tuples). – user18387401 May 09 '22 at 08:12
  • 1
    I already read this, that's why I'm here. Either it doesn't answer my question, or it is not possible to do in VB. – Virus721 May 09 '22 at 08:14
  • 1
    Or you misunderstood what it says. You need to use Tuples to return multiple values in .NET, whether you use C# or VB.NET. What you really ask isn't how to return multiple values but how to deconstruct them. Tuples are *not* anonymous. `var` doesn't mean the type is unknown, it means it's inferred by the compiler. You could use `var (someInt,someString)` in C# but the actual result would be `(int someInt,string someString)` – Panagiotis Kanavos May 09 '22 at 08:41
  • 3
    VB.NET doesn't have deconstructors, either typed or anonymous. There's a [github issue](https://github.com/dotnet/vblang/issues/346) in the [vblang](https://github.com/dotnet/vblang) repository where people discuss this with various workarounds, but the feature itself hasn't been implemented. – Panagiotis Kanavos May 09 '22 at 08:53
  • 1
    @Panagiotis Kanavos Thanks ! Yes I know it's tuples internally, but in C# you don't have to name them, the compiler is sort of hiding them from you. I'll have to wait for the next VB version. – Virus721 May 09 '22 at 09:01
  • Or, you could just use C# if you want leading edge features? ;) – Caius Jard May 09 '22 at 10:38
  • As inconveniences go, this one is relatively mild. You can do an inferred assignment to a combined tuple, and it's low overhead to access the members through the name of the tuple (especially with intellisense). – Craig May 09 '22 at 14:00
  • I was more focused on code elegance than performance here. I think being forced to use some sort of intermediate identifier defeats the purpose of "multiple return values". At this point I could just write a specific structure and it would be almost the same. – Virus721 May 10 '22 at 08:29

0 Answers0