CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2006
    Posts
    28

    Function Return Types

    I am making a function and I want it to return an array if it is a success otherwise a boolean of false. Is there a way I can do this?

    Function test()
    If ( mystuff = Good) Then
    Return myArray
    Else
    Return False
    End If
    End Function

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Function Return Types

    You cannot have a function that returns two different things. If you can explain a bit, probably someone can suggest a better solution.

  3. #3
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Function Return Types

    You can always return a structure that contains both the boolean, lets call it isArrayValid, and the array... thus, after the function returns, you can test if the array content is valid and can be used.
    Still, I'd recommend passing the array as an "out" parameter (ByRef) for the function... which returns the status as a boolean, or even an error code, which enables the caller to find out what went wrong when the array content isn't valid.
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Function Return Types

    You could pass an array as a reference and return a boolean like in this example:
    Code:
    Function DoIt(ByRef A() As Integer) As Boolean
      ReDim A(2)
      A(0) = 1
      A(1) = 2
      DoIt = True
    End Function
    
    dim Arr() as integer
    If DoIt(Arr) Then debug.print Arr(0), Arr(1)
    @Bornish: Oh. Hi. Didn't see your post, when replying.

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Function Return Types

    Quote Originally Posted by Bornish
    You can always return a structure that contains both the boolean, lets call it isArrayValid, and the array... thus, after the function returns, you can test if the array content is valid and can be used.
    Still, I'd recommend passing the array as an "out" parameter (ByRef) for the function... which returns the status as a boolean, or even an error code, which enables the caller to find out what went wrong when the array content isn't valid.
    Good one

    This was on my mind too, but I wanted to listen to fist what the actual requirement is.

  6. #6
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Function Return Types

    or you may declare a variant-returning function and use vartype to determine the return value


    Code:
    private sub callingSub()
    dim ret as variant
    
      ret = testFunction()
    
      if vartype(ret) = vbboolean then
        ' boolean
      else
        ' array or other type
      end if   
    
    end sub
    
    
    private function testFunction() as variant  
      ' some codes
    end function
    Busy

  7. #7
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Function Return Types

    Maybe it's even possible to return Nothing. So, when the function fails return Nothing, and when the function succeeds return the array (possibly an empty one).

    - petter

  8. #8
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Function Return Types

    Quote Originally Posted by wildfrog
    Maybe it's even possible to return Nothing. So, when the function fails return Nothing, and when the function succeeds return the array (possibly an empty one).

    - petter
    Nothing will not work for Arrays in VB 6. Thread1's solution is a better one using Variants.

  9. #9
    Join Date
    Sep 2006
    Posts
    28

    Re: Function Return Types

    Quote Originally Posted by Thread1
    or you may declare a variant-returning function and use vartype to determine the return value


    Code:
    private sub callingSub()
    dim ret as variant
    
      ret = testFunction()
    
      if vartype(ret) = vbboolean then
        ' boolean
      else
        ' array or other type
      end if   
    
    end sub
    
    
    private function testFunction() as variant  
      ' some codes
    end function
    This is my code and its always returning a "Not" messagebox:

    Code:
    Private function testFunction() As Variant
      testFunction = False
    End function
    
    private sub callingSub()
    dim ret as variant
    
      ret = testFunction()
    
      if (vartype(ret) = vbboolean) then
        MsgBox "Boolean"
      else
        MsgBox "Not"
      end if   
    end sub
    Last edited by danbopes; May 17th, 2007 at 08:08 AM.

  10. #10
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Function Return Types

    if you see your code closely, the name of the function that you have written is textFunction while as in the CallingSub procedure you are calling ret = testFunction().

    Does your code Execute properly. Doesn't it throw any error?

  11. #11
    Join Date
    Sep 2006
    Posts
    28

    Re: Function Return Types

    Yeah I figured it out, for some reason my ADODB result set it always returning false for result.EOF even if there is no rows.

  12. #12
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Function Return Types

    Can you post the code here, so that we can take a look at it.

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

Featured