Results 1 to 11 of 11

Thread: Return values from a function

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Location
    Dhaka, Bangladesh
    Posts
    102

    Return values from a function

    I want to return four values (more than one) from a function to its caller.

    Four values are of four datatypes(int, long, double, string).

    I just know how to return one value from a function. But how can I return more than one ?

    Is it possible ?

    plz, Give an example.

    --Rajib

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Return values from a function

    technically a function is only meant to return one value, so you should create a class or structure that will hold your four values.
    You could use the out keyword, but it's not a good solution

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Location
    Dhaka, Bangladesh
    Posts
    102

    Re: Return values from a function

    Thanks for your help.

    But I have found that it is easily solved by Reference passing.


    ---Rajib

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Return values from a function

    Something to bear in mind an argument passed by ref must be first initialised. An out does not have to be initialised.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Return values from a function

    Can you tell me more about the out keyword?

  6. #6
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Return values from a function

    further reading:
    out

    ref

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Return values from a function

    Ah, so the out keyword is quite like those from SPs. Yet another feature I wish VB.NET had.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Return values from a function

    ref exists in VB.NET. When you pass an argument to a function, you can do it ByVal or ByRef.

  9. #9
    Addicted Member
    Join Date
    Jan 2005
    Location
    Montréal
    Posts
    160

    Re: Return values from a function

    An interesting thing with C# is that using out, ref or neither of them for a given function parameter defines different function signatures. This allows the overloading of a function in this way :

    Code:
            void testfunction(int iVal)
            {
            }
    
            void testfunction(ref int iVal)
            {
            }
    
            void testfunction(out int iVal)
            {
            }
    All is well if used only in C#, but you cannot use this function in VB through a compiled assembly because it is ambiguious in VB because in VB you don't tell the compiler that you want to send your function parameter ByVal or ByRef.
    There are no stupid questions, but a whole bunch of dumb sayings !

    Save time on database code, try DataLG !

  10. #10
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Return values from a function

    Code:
    //this is a valid overload
    void testfunction(int iVal){}
    void testfunction(ref int iVal){}
    
    //so is this
    void testfunction(int iVal){}
    void testfunction(out int iVal){}
    
    //this is not a valid overload
    void testfunction(ref iVal){}
    void testfunction(out int iVal){}

  11. #11
    Addicted Member
    Join Date
    Jan 2005
    Location
    Montréal
    Posts
    160

    Re: Return values from a function

    My bad, I assumed that it would be valid too, I never tried to mix out and ref before...
    There are no stupid questions, but a whole bunch of dumb sayings !

    Save time on database code, try DataLG !

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