Multiple Returns From A Single Function

Kayot

Well-known member
Joined
Mar 6, 2007
Messages
49
Programming Experience
3-5
This post sort of covers what I wanted to know:
http://www.vbdotnetforums.com/showthread.php?t=13167&highlight=function

However, I was wondering it it's possible to get multiple returns of the same type. Heres the block of code that I wanted to use it on.

VB.NET:
#Region " I'd like to make this a function "
    Dim bytProcess1 As Byte = 0
    Dim bytProcess2 As Byte = 0
    Dim bytProcess3 As Byte = 0
    Private Sub ByteProcess(ByVal Value As Long)
        bytProcess3 = Value \ 65536
        Value = Value - bytProcess3 * 65536
        bytProcess2 = Value \ 256
        Value = Value - bytProcess2 * 256
        bytProcess1 = Value
    End Sub
#End Region

I'd like to get it to return 3 byte size chunks. Kind of like this non-working code here.


VB.NET:
#Region " I'd like to make this a function "
    'Dim bytProcess1 As Byte = 0
    'Dim bytProcess2 As Byte = 0
    'Dim bytProcess3 As Byte = 0
    Private Function ByteProcess(ByVal Value As Long) as bytProcess1 ,bytProcess2 ,bytProcess3 as Byte
        bytProcess3 = Value \ 65536
        Value = Value - bytProcess3 * 65536
        bytProcess2 = Value \ 256
        Value = Value - bytProcess2 * 256
        bytProcess1 = Value
    End Sub
#End Region

The idea is that when it's called it looks like this.

VB.NET:
Call ByteProcess(Original.Text, Byte1.Text, Byte2.Text, Byte3.Text)

I don't think it's possible, but last time I thought that, DirectCast jumped in and kicked my precognitions.
 
What about ByRef?

Hi,
What about ByRef?


VB.NET:
Module Module1


    Dim bytProcess1 As Byte = 0
    Dim bytProcess2 As Byte = 0
    Dim bytProcess3 As Byte = 0


    Sub Main()
        ByteProcess(75536, bytProcess1, bytProcess2, bytProcess3)
        Console.WriteLine(bytProcess1.ToString() & " " & bytProcess2.ToString() & " " & bytProcess3.ToString())
        Console.ReadLine()
    End Sub


    Private Function ByteProcess(ByVal Value As Long, ByRef bytProcess1 As Byte, ByRef bytProcess2 As Byte, ByRef bytProcess3 As Byte)
        bytProcess3 = Value \ 65536
        Value = Value - bytProcess3 * 65536
        bytProcess2 = Value \ 256
        Value = Value - bytProcess2 * 256
        bytProcess1 = Value
    End Function

End Module
 
I was wondering it it's possible to get multiple returns of the same type.
That is called an "array".
VB.NET:
    Private Function ByteProcess(ByVal Value As Long) As Byte()
        Dim bytProcess1 As Byte = 0
        Dim bytProcess2 As Byte = 1
        Dim bytProcess3 As Byte = 2
        Dim ar() As Byte = {bytProcess1, bytProcess2, bytProcess3}
        Return ar
    End Function
The ByRef suggestion is also valid.
 
Cool, It worked!

Thank you for answering my n00b questions. ^-^ Now that I can use this the whole world of programming has opened up.

In two days I learned about Functions that can do so much. Four days ago I made my first custom control, I can hardly wait till tomorrow :)
 
Note that multiple returns froma single function usually represents a flaw in OO design. One function should have one purpose and hence one return value. In this case, the Byte array suggestion is most sensible. There are only a few places where using ref parameters makes sense, and it is usually when crossing into the boundaries of another system or process (e.g. API calls, DB stored procedure access). Try to avoid using ref parameters wherever possible; it's not great OO. If you have multiple complex things to return from a function, it indicates that a custom class is required..
 
Back
Top