Results 1 to 6 of 6

Thread: [2005] return 2 values from a function?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    [2005] return 2 values from a function?

    how do u make it so instead of returning one value from a function u can return 2?

  2. #2
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: [2005] return 2 values from a function?

    You have to use an output parameter, you cannot return two values the traditional way.

    Public Function MyFunction(ByRef returnValue2 As Double) As Double

    The second value can be returned through returnValue2. Make sense?
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2005] return 2 values from a function?

    Quote Originally Posted by DNA7433
    You have to use an output parameter, you cannot return two values the traditional way.

    Public Function MyFunction(ByRef returnValue2 As Double) As Double

    The second value can be returned through returnValue2. Make sense?

    not really...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] return 2 values from a function?

    A function returns one object; no more, no less. That's the definition of a function: a method that returns a value. Now having said that a function returns one object, that object can be whatever you like. It can be an instance of a structure or class that have multiple properties that each return individual objects, or it can be an array or collection that contains multiple objects.

    Alternatively you can do what DNA suggests and pass one or more parameters by reference, so that their value can be set withing the function and then passed back out again. An example of this is the Date.TryParse method. It needs to pass out two values: a boolean to indicate whether the operation was successful and the Data object that was created if it was successful. To do this its return type is Boolean and it has a Date argument passed by reference. That means that if the operation is successful, that parameter contains the Date after the method returns, e.g.
    VB Code:
    1. Dim returnValue As Boolean
    2. Dim byRefParameter As Date
    3.  
    4. returnValue = Date.TryParse(someString, byRefParameter)
    5.  
    6. If returnValue = True Then
    7.     'The operation was successful so the date value is contained in the second parameter.
    8.     MessageBox.Show("The data is " & byRefParameter.ToString())
    9. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] return 2 values from a function?

    Or you can create a structure containing variables to hold the values that you need then have your function return the structure.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] return 2 values from a function?

    Quote Originally Posted by stanav
    Or you can create a structure containing variables to hold the values that you need then have your function return the structure.
    I agree.
    Quote Originally Posted by jmcilhinney
    that object can be whatever you like. It can be an instance of a structure or class that have multiple properties that each return individual objects
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width