Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Function () with multiple Return values

Posted on 2007-09-10
10
Medium Priority
?
1,810 Views
Last Modified: 2009-07-10
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
0
Comment
Question by:Mr_Fulano
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Assisted Solution

by:adam_pedley
adam_pedley earned 500 total points
ID: 19865197
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 19865343
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
 

Author Comment

by:Mr_Fulano
ID: 19865567
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Mr_Fulano
ID: 19865595
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 19865603
use ByRef parameters. They are made for this.
0
 

Author Comment

by:Mr_Fulano
ID: 19865613
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
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 500 total points
ID: 19865914
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
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1000 total points
ID: 19867106

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
 

Author Comment

by:Mr_Fulano
ID: 19873733
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 19874808
glad to be of assistance

AW
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question