Function () with multiple Return values

Hi, I have a Function () that performs some calculations and then comes up with 4 different values that I need to use in my code. I return the values as listed below, and it works, but I'm not sure if there is a more elegant way to do it.  I tried [Return value1, value2, value3, value4], but it gave me an error.

Function ()
   Calculate some values...
 Return value1
 Return value2
 Return value3
 Return value4
End Function

Is there a better way?


Thanks,
Fulano
Mr_FulanoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adam_pedleyCommented:
The above code should not work.

It will return the first value then break from the function. A return exits the function with the value.

To return multiple values create a class or array
Use an array for objects of the same type
or a user defined class for objects of different types with only a set number of return values
For example

    Public Function Test() As Values
        Dim v As New Values
        v.Value1 = "Somevalue"
        Return v
    End Function

End Class

Public Class Values
    Private _value1 As String
    Public Property Value1() As String
        Get
            Return _value1
        End Get
        Set(ByVal value As String)
            _value1 = value
        End Set
    End Property
End Class


or you can use an array

Dim v(2) as string
v(0) = "Value1"
v(1) = "Value2"
v(2) = "Value3"

or a collection object

Dim values as new Collections.ArrayList
values.add("somevalue")

Look through the Collections namespace for other collections
0
Éric MoreauSenior .Net ConsultantCommented:
You can also use ByRef parameters:

Function (byref value1, byref value2, byref value3, byref value4)
   Calculate some values...
 value1 = ...
 value2 = ...
 value3 = ...
 value4 = ...
End Function
0
Mr_FulanoAuthor Commented:
Actually Adam, your correct, its only returning the first function...I thought it would crash if it didn't work, but it just returns value1 and drops out. It does however, print to console.writeline all 4 value from within the function, that's how I know they are correct values.

All the values are Integers, so let me play with some of your suggestions and I'll get back to you soon...hopefully I can use some sort of array or UD class as you've suggested.

Thanks,
FDT

0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Mr_FulanoAuthor Commented:
Just to provide a bit more info, I pass my Function two variables, one is an Integer and one is a string. From there I get four Integers that I need to use in my application.  Something like this...

Function Test (ByVal i as Integer, ByVal myString as String)
    value1 = i+ myString.Lenght * 2
    value2 = i * myString.Lenght * myString.Lenght * myString.Lenght  
    value3 = i / myString.Lenght * myString.Lenght / 5
    value4 = i - myString.Lenght  * myString.Lenght  
    ...whatever

 Return value1
 Return value2
 Return value3
 Return value4
End Function
0
Éric MoreauSenior .Net ConsultantCommented:
use ByRef parameters. They are made for this.
0
Mr_FulanoAuthor Commented:
Hi Emoreau,

In your example below, how do I pass the values(1-4) out of the Function...? Is it like a Subroutine that it would just pass the new values to global variables?

For example, if I need to get the my 4 values with your example. How would you do it if I'm passing 2 variables in and getting 4 values out?  -- So, assuming my Function is called get4Values(), I would pass as follows:  get4Values(i, myString). I need to get 4 Integers back...

Emoreau's example is below:
Function (byref value1, byref value2, byref value3, byref value4)
   Calculate some values...
 value1 = ...
 value2 = ...
 value3 = ...
 value4 = ...
End Function

0
Arthur_WoodCommented:
to be strictly Object Oriented, you should create your own Custom class, and have the function return an instance of that custom class.  Like this:

Public Class MyCustomClass
   Dim _firstVal as Integer
   Dim _secondVal as Integer
   Dim _thirdVal as Integer
   Dim _fourthVal as Integer

Public Property FirstVal() as Integer
    Get
        Return _firstVal
    End Get
    Set (ByVal RHS as Integer)
       _firstVal = RHS
    End Set
End Property

Public Property SecondVal() as Integer
    Get
        Return _secondVal
    End Get
    Set (ByVal RHS as Integer)
       _secondVal = RHS
    End Set
End Property
   
Public Property ThirdVal() as Integer
    Get
        Return _thirdVal
    End Get
    Set (ByVal RHS as Integer)
       _thirdVal = RHS
    End Set
End Property

Public Property FourthVal() as Integer
    Get
        Return _fourthVal
    End Get
    Set (ByVal RHS as Integer)
       _fourthVal= RHS
    End Set
End Property

End Class


then your function would look like this:

Public Function() as MyCustomClass
    Dim _myCustomInstance as MyCustomClass = New MyCustomClass

    _myCustomInstance .FirstVal = ...
    _myCustomInstance .SecondVal = ...
    _myCustomInstance .ThirdVal = ...
    _myCustomInstance .FourthVal = ...

    Return _myCustomInstance

End Function

Or you could add a Constructor to your class, which takes four integers as the arguments:

   Public Sub New(byVal FirstVal as Integer, byVal SecondVal as Integer, _
           byVal ThirdVal as Integer, byVal FourthVal as Integer)
   _firstVal = FirstVal
   _secondVal = SecondVal
   _thirdVal = ThirdVal
   _fourthVal = FourthVal
End Sub

and then in the function:

    Public Function() as MyCustomClass
    .
    .
    .
    Return New MyCustomClass(value1, value2, value3, value4)
End Function

Your choice.

AW
0
Éric MoreauSenior .Net ConsultantCommented:

dim a, b, c, d as integer
dim i as integer
dim myString as string
i = something
myString = SomethingElse
get4Values(i, myString, a, b, c, d)
'here you can get values a, b, c, d returned from get4Values

Emoreau's example is below:
Function  get4Values(byval p1 as integer, byval p2 as string, byref value1, byref value2, byref value3, byref value4)
   Calculate some values...
 value1 = ...
 value2 = ...
 value3 = ...
 value4 = ...
End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mr_FulanoAuthor Commented:
Gentlemen, thanks for the help.  I think that Emoreau's function does what I needed in a simple manner. I know that there are differences between ByVal and ByRef, but I'm not sure it will affect my code to any degree for what I need to do. Having said that, I also liked and will use the Class approach when appropriate, so thank you for that. I've increased points and split them accordingly.

Thanks,
Fulano
0
Arthur_WoodCommented:
glad to be of assistance

AW
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.