Multiple Return Values in Visual Basic .NET

Visual Basic .NET has built-in support for multiple return values through tuples. This feature is often used to return both result and error values from a function.

Imports System

Module Program
    ' The (Integer, Integer) in this function signature shows that
    ' the function returns 2 Integers.
    Function Vals() As (Integer, Integer)
        Return (3, 7)
    End Function

    Sub Main()
        ' Here we use the 2 different return values from the
        ' call with multiple assignment.
        Dim (a, b) = Vals()
        Console.WriteLine(a)
        Console.WriteLine(b)

        ' If you only want a subset of the returned values,
        ' you can ignore one of the values by using a discard.
        Dim (_, c) = Vals()
        Console.WriteLine(c)
    End Sub
End Module

To run the program, save it as MultipleReturnValues.vb and use the VB.NET compiler:

$ vbc MultipleReturnValues.vb
$ MultipleReturnValues.exe
3
7
7

In Visual Basic .NET, we use tuples to return multiple values from a function. The Vals function returns a tuple of two integers. When calling the function, we can use tuple deconstruction to assign the returned values to separate variables.

The _ in (_, c) is called a discard. It’s used when you want to ignore one of the returned values. This is similar to the blank identifier in other languages.

Visual Basic .NET also supports other advanced features like optional parameters and named arguments, which can provide additional flexibility when working with functions.