Multiple Return Values
This page was last reviewed on Feb 18, 2022.
Dot Net Perls
Multiple return values. A function in VB.NET can only return one value—this can be a value type or a reference type (like a Class). But one value is not always sufficient.
With ByRef arguments, we can set multiple output values. And with a structure like KeyValuePair (or a class like Tuple) we can return multiple values as one.
ByVal, ByRef
An example program. Let us start with this program: it has two methods both named TodayAndTomorrow. These both "return" two DateTime Structure values.
Detail The first version uses ByRef. It sets the two output parameters before it exits. We can use them in the calling location.
Detail The second version uses KeyValuePair. It returns a KeyValuePair created by calling the New KeyValuePair constructor.
Module Module1 Sub TodayAndTomorrow(ByRef today As DateTime, ByRef tomorrow As DateTime) today = DateTime.Today tomorrow = DateTime.Today.AddDays(1) End Sub Function TodayAndTomorrow() As KeyValuePair(Of DateTime, DateTime) Return New KeyValuePair(Of DateTime, DateTime)( DateTime.Today, DateTime.Today.AddDays(1)) End Function Sub Main() Dim day1 As DateTime Dim day2 As DateTime TodayAndTomorrow(day1, day2) Console.WriteLine(day1) Console.WriteLine(day2) Console.WriteLine() Dim pair As KeyValuePair(Of DateTime, DateTime) = TodayAndTomorrow() Console.WriteLine(pair.Key) Console.WriteLine(pair.Value) End Sub End Module
12/14/2014 12:00:00 AM 12/15/2014 12:00:00 AM 12/14/2014 12:00:00 AM 12/15/2014 12:00:00 AM
Tuple return values. Let's use the Tuple class to return multiple values from a Function. A tuple can return many more than two values, but we just show two here.
Tip The Tuple is used in the same way as a KeyValuePair. It can return three, four, or more values with the same syntax form.
Module Module1 Function TodayAndTomorrow() As Tuple(Of DateTime, DateTime) Return New Tuple(Of DateTime, DateTime)( DateTime.Today, DateTime.Today.AddDays(1)) End Function Sub Main() Dim t As Tuple(Of DateTime, DateTime) = TodayAndTomorrow() Console.WriteLine(t.Item1) Console.WriteLine(t.Item2) End Sub End Module
12/14/2014 12:00:00 AM 12/15/2014 12:00:00 AM
In my experience, ByRef arguments are easier to use. This probably depends on how a programmer is thinking. But introducing Tuples or Pairs seems excessively complex to me.
And The concept of reference, output parameters is well-known in the programming world.
So Using a ByRef argument to return a value will yield code that is easily understood by others.
For performance, though, my previous benchmarks have found KeyValuePair to be a good choice. It is a Structure so it requires no heap allocation.
And ByRef arguments tend to defeat certain compiler optimizations. The JIT compiler in the .NET Framework has limits.
A summary. With ByRef, KeyValuePair and Tuple, we return many values from VB.NET Subs and Functions. This approach is powerful. Not all methods logically return just 1 value.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 18, 2022 (edit link).
© 2007-2024 Sam Allen.