A Function To Return Two Values

Hi,

Is it possible for a function to return two "things" or items, i.e. a data item the function is used to get (a string) and a result saying if the function was successful or not (boolean)

What I am trying to do is parse through an XML file from a server (where lots of things could go wrong), and use the data in my application by calling a function which does the parsing. I need to be able to display an error message in the application if the parsing function goes wrong.

Thanks.



Answer this question

A Function To Return Two Values

  • Shailesh

    I'd return a structure containing the return value, status, and any additional useful data - such as any exception thrown.


  • Coqui

    Structures are new in VB.Net, and replaced VB6's User Defined Types.

    In .Net, structures are like lightweight classes, but they are value types rather than reference types, so they don't incur some of the overheads of classes, though they can be expensive to pass about if they are large.  A short structure containing a return value and a string (itself a reference type, and therefore only taking up a reference in the structure) should be fine.

    As Jim Wooley says, you should check to see whether your needs are better met by using the .net structured error handling.

    My take on this one:

    If it's a case of either you get a string return, or you get an error indication, then you should use the error handling.   If you get a string return and one of several possible status values indicating the status of the string and/or further action to be taken, you should use either a structure or reference parameters.

    Personally, I'm not a fan of functions that change their reference parameters - particularly in a function that also returns a value - they represent a side-effect that is not immediately apparent when reading the calling code.  Don't get me wrong, it's an acceptable technique, and it works, but returning a structure is more explicit.

     

     


  • tmike

    Thanks very much to everyone who has replied to my question, you've all really helped me.

    I've now managed to use a Try...Catch system in my code to handle errors, so everything works fine now.

    Once again, thanks



  • David Veeneman

    I agree with Jim. Functions should not return false when unsuccsessful. They should throw an exception that is handled by the caller. This is a common design rule.

    Another rule to consider is not to use ByRef with functions. A function should return ONE result. If two results are to be return, then a method with two ByRef should be used, not a function with one ByRef. Do you follow

    Correct:

    Private Function CorrectFunction(ByVal inArg As Integer) As Integer

    Private Sub CorrectMethod(ByVal inArg As Integer, ByRef outResult1 As Integer, ByRef outResult2 As String)

    Incorrect:

    Private Function IncorrectFunction(ByVal inArg As Integer, ByRef outResult1 As String) As Integer


  • LukaSoFt

    It is possible to have a function return multiple values if you use parameters specified ByRef. When an object is passed ByRef, changes made to it within the function affect the actual object being passed and are thus visible to the calling code.

    That being said, It would seem that the better option in this case is to take advantage of structured error handling. IS your parsed result valid if there was a parsing error If not, your parsing routine would throw an exception if something "goes wrong" in the parsing. Your application then wraps the call to the parse routine in a Try...Catch block. You can display the error message as part of the catch block handling routine. In this case, you ignore the returned results of the function, but this should be acceptable since there is a bigger problem anyway.

    Jim Wooley
    http://devauthority.com/blogs/jwooley



  • kfarley215

    Hi

    It is not possible for a function to literally return two values but there are ways of achieving this. Firstly however, you state that your function will return a string and you also want it to return a boolean to determine if the function succeeded. Could your function not return either a String value if it succeeded or Nothing if it failed

    Public Function SomeFunction() As String

    Dim returnValue As String = Nothing

    'Process your logic here and set the returnValue if required

    Return returnValue

    End Function

    Then in your calling code you can simply check if the returned string is nothing (function failed) or has a value (function succeeded).

    As to returning more than one value from a function, you could have your function return a class and this class would contain the values (properties) that would be set via the function. Therefore, your function will still only return a single value but that value will be your custom class that could contain many values.

    HTH


  • Forum Coder

    Thanks David Jeavons.

    @ Dave Biggins,

    What do you mean by structure Do you mean a class, or is a structure something I haven't heard of yet (I am very new to Visual Basic and programming in general!)

    Thanks.


  • A Function To Return Two Values