VB.NET Multiple Return Values

Return multiple values with the ByRef keyword and the KeyValuePair structure.

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.Function

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.Structure
ByRef: The first version uses ByRef. It sets the two output parameters before it exits. We can use them in the calling location.
KeyValuePair: The second version uses KeyValuePair. It returns a KeyValuePair created by calling the New KeyValuePair constructor.
VB.NET program that uses multiple return values 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 Output 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.Tuple
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.
VB.NET program that uses Tuple 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 Output 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.


© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.