Pages

Wednesday 4 April 2012

Return multiple variables from VB.NET function


Problem:

How to return multiple variables from a single method call?

Scenario:

To fetch error messages with the warning level based on the error code, there could be more scenarios in similar fashion. Almost every developer will be muddled for a moment how [?] and start thinking to overcome the situation in a better way.

Problem Code
Public Function GetErrorMessage(errorCode As IntegerAs ??
     Dim errorMessage As String = "How to return multiple values!"
     Dim errorWarningLevel As Integer = 3

     Return errorMessage
     Return errorWarningLevel
End Function


Queries while implementing the above code,

  •        What could be the return type [String/Integer/ etc…]?
  •        If we write multiple return code lines, will it return to the calling method what we expect?


There have been lots of workarounds to handle this scenario here are few,

  •        Split the method into two business operations/methods
  •        Create a type which holds the required properties and return the type
  •        Adding in a list/hash-table and read the variables looping or with specific key
  •        Return as object [base type] and type cast in the calling method


Couple of advanced coding workarounds,

  •        Create an anonymous type in the called method and return as object, caller will type cast and convert into specific type
  •        Create an [Iterator] function and [Yield] the return variables/types, we can implement this using C# and also added as new feature in VB11

  Workaround:

Here is simple data structure introduced in .NET framework 4.0 TUPLE
Tuple can hold up to 8 different types in a single instance and return back to calling method, it not only holds the values but also acts as factory class that create instance of its static members.

Workaround Code
Private Sub MyTuple_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load
        Dim errorDetails = GetErrorMessage(555)
        Dim errorMessage = errorDetails.Item2 'Item2 holds Error message in Tuple
        Dim errorWarningLevel = errorDetails.Item3 'Item2 holds Error WarningLevel in Tuple
    End Sub
 
Public Function GetErrorMessage(errorCode As IntegerAs Tuple(Of IntegerStringInteger)
        Dim errorMessage As String = "How to return multiple variables in a single method call!"
        Dim errorWarningLevel As Integer = 3
 
        'Commented old code for reference
        'Return errorMessage
        'Return errorWarningLevel
 
        'Option 1: using TUPLE helper method 
        Dim errorDetails = Tuple.Create(errorCode, errorMessage, errorWarningLevel)
 
        'Option 2: Using TUPLE constructor
        Dim errorDetails As New Tuple(Of IntegerStringInteger)(errorCode, errorMessage, errorWarningLevel)
 
        Return errorDetails
 
    End Function


Advantages of using Tuples:

  •        Strongly typed
  •        Can hold any defined type [Structured  data types]
  •        Implements IStructuralComparable and IStructuralEquatable

No comments:

Post a Comment