Returning an array from a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim SUTTON

    Returning an array from a function

    I am sure I have had this problem before and can't remember what the fix
    is...

    The documentation says that an array can be returned from a function
    provided you do the following:-

    1. Declare the function with an explicit return type - the examples suggest
    it also needs the parentheses to denote a returned array thus:

    Private Function GetResultArray( RawResults as String, RequiredResultS etName
    as String) as String()

    2. Return the array (without parentheses) as part of the Exit Function
    statement thus:

    Exit Function MyTempArray

    Why do I always get a syntax error on the Exit Function statement, with the
    line compiler complaining it is expecting end of statement after Exit
    Function?

    Any help appreciated...

    Regards

    --Jim.


  • Stephane Richard

    #2
    Re: Returning an array from a function

    Try having the function return a variant instead of a string()

    --
    Stéphane Richard
    "Ada World" Webmaster



    "Jim SUTTON" <jimSpamMeNot@b radleysutton.ne t> wrote in message
    news:LMbgb.2073 $ee6.1077@news-binary.blueyond er.co.uk...[color=blue]
    > I am sure I have had this problem before and can't remember what the fix
    > is...
    >
    > The documentation says that an array can be returned from a function
    > provided you do the following:-
    >
    > 1. Declare the function with an explicit return type - the examples[/color]
    suggest[color=blue]
    > it also needs the parentheses to denote a returned array thus:
    >
    > Private Function GetResultArray( RawResults as String,[/color]
    RequiredResultS etName[color=blue]
    > as String) as String()
    >
    > 2. Return the array (without parentheses) as part of the Exit Function
    > statement thus:
    >
    > Exit Function MyTempArray
    >
    > Why do I always get a syntax error on the Exit Function statement, with[/color]
    the[color=blue]
    > line compiler complaining it is expecting end of statement after Exit
    > Function?
    >
    > Any help appreciated...
    >
    > Regards
    >
    > --Jim.
    >
    >[/color]


    Comment

    • Jim SUTTON

      #3
      Re: Returning an array from a function

      OK, tried that BUT without any success - I must have missed something
      somewhere else if this is working for you.

      Thanks and regards

      --Jim.
      "Stephane Richard" <stephane.richa rd@verizon.net> wrote in message
      news:Oncgb.2790 6$yU5.7476@nwrd ny01.gnilink.ne t...[color=blue]
      > Try having the function return a variant instead of a string()
      >
      > --
      > Stéphane Richard
      > "Ada World" Webmaster
      > http://www.adaworld.com
      >
      >
      > "Jim SUTTON" <jimSpamMeNot@b radleysutton.ne t> wrote in message
      > news:LMbgb.2073 $ee6.1077@news-binary.blueyond er.co.uk...[color=green]
      > > I am sure I have had this problem before and can't remember what the fix
      > > is...
      > >
      > > The documentation says that an array can be returned from a function
      > > provided you do the following:-
      > >
      > > 1. Declare the function with an explicit return type - the examples[/color]
      > suggest[color=green]
      > > it also needs the parentheses to denote a returned array thus:
      > >
      > > Private Function GetResultArray( RawResults as String,[/color]
      > RequiredResultS etName[color=green]
      > > as String) as String()
      > >
      > > 2. Return the array (without parentheses) as part of the Exit Function
      > > statement thus:
      > >
      > > Exit Function MyTempArray
      > >
      > > Why do I always get a syntax error on the Exit Function statement, with[/color]
      > the[color=green]
      > > line compiler complaining it is expecting end of statement after Exit
      > > Function?
      > >
      > > Any help appreciated...
      > >
      > > Regards
      > >
      > > --Jim.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Andreas

        #4
        Re: Returning an array from a function

        Just send a reference to the array to a sub. For example:

        Public Sub calculateData(B yRef myData)
        myData(5) = 1
        end sub

        sub Main()
        Dim myData(0 to 100) as integer
        Call calculateData(B yRef myData)
        Debug.print myData(5)
        end sub

        "Jim SUTTON" <jimSpamMeNot@b radleysutton.ne t> wrote in message news:<LMbgb.207 3$ee6.1077@news-binary.blueyond er.co.uk>...[color=blue]
        > I am sure I have had this problem before and can't remember what the fix
        > is...
        >
        > The documentation says that an array can be returned from a function
        > provided you do the following:-
        >
        > 1. Declare the function with an explicit return type - the examples suggest
        > it also needs the parentheses to denote a returned array thus:
        >
        > Private Function GetResultArray( RawResults as String, RequiredResultS etName
        > as String) as String()
        >
        > 2. Return the array (without parentheses) as part of the Exit Function
        > statement thus:
        >
        > Exit Function MyTempArray
        >
        > Why do I always get a syntax error on the Exit Function statement, with the
        > line compiler complaining it is expecting end of statement after Exit
        > Function?
        >
        > Any help appreciated...
        >
        > Regards
        >
        > --Jim.[/color]

        Comment

        • Jim SUTTON

          #5
          Re: Returning an array from a function

          This seems to work OK - thanks!

          Regards

          --Jim.

          "Andreas" <d99alu@efd.lth .se> wrote in message
          news:f7cd4365.0 310060532.3a8e6 923@posting.goo gle.com...[color=blue]
          > Just send a reference to the array to a sub. For example:
          >
          > Public Sub calculateData(B yRef myData)
          > myData(5) = 1
          > end sub
          >
          > sub Main()
          > Dim myData(0 to 100) as integer
          > Call calculateData(B yRef myData)
          > Debug.print myData(5)
          > end sub
          >
          > "Jim SUTTON" <jimSpamMeNot@b radleysutton.ne t> wrote in message[/color]
          news:<LMbgb.207 3$ee6.1077@news-binary.blueyond er.co.uk>...[color=blue][color=green]
          > > I am sure I have had this problem before and can't remember what the fix
          > > is...
          > >
          > > The documentation says that an array can be returned from a function
          > > provided you do the following:-
          > >
          > > 1. Declare the function with an explicit return type - the examples[/color][/color]
          suggest[color=blue][color=green]
          > > it also needs the parentheses to denote a returned array thus:
          > >
          > > Private Function GetResultArray( RawResults as String,[/color][/color]
          RequiredResultS etName[color=blue][color=green]
          > > as String) as String()
          > >
          > > 2. Return the array (without parentheses) as part of the Exit Function
          > > statement thus:
          > >
          > > Exit Function MyTempArray
          > >
          > > Why do I always get a syntax error on the Exit Function statement, with[/color][/color]
          the[color=blue][color=green]
          > > line compiler complaining it is expecting end of statement after Exit
          > > Function?
          > >
          > > Any help appreciated...
          > >
          > > Regards
          > >
          > > --Jim.[/color][/color]


          Comment

          • Randy Birch

            #6
            Re: Returning an array from a function

            Private Sub Command1_Click( )

            Dim x() As String

            x = DoSomething("he llo", "world")

            Print x(0)
            Print x(1)

            End Sub


            Private Function DoSomething(p1 As String, p2 As String) As String()

            Dim buff() As String

            ReDim buff(0 To 1) As String

            buff(0) = p1
            buff(1) = p2

            DoSomething = buff

            End Function



            --

            Randy Birch
            MVP Visual Basic

            Please respond only to the newsgroups so all can benefit.


            "Jim SUTTON" <jimSpamMeNot@b radleysutton.ne t> wrote in message
            news:LMbgb.2073 $ee6.1077@news-binary.blueyond er.co.uk...
            : I am sure I have had this problem before and can't remember what the fix
            : is...
            :
            : The documentation says that an array can be returned from a function
            : provided you do the following:-
            :
            : 1. Declare the function with an explicit return type - the examples
            suggest
            : it also needs the parentheses to denote a returned array thus:
            :
            : Private Function GetResultArray( RawResults as String,
            RequiredResultS etName
            : as String) as String()
            :
            : 2. Return the array (without parentheses) as part of the Exit Function
            : statement thus:
            :
            : Exit Function MyTempArray
            :
            : Why do I always get a syntax error on the Exit Function statement, with
            the
            : line compiler complaining it is expecting end of statement after Exit
            : Function?
            :
            : Any help appreciated...
            :
            : Regards
            :
            : --Jim.
            :
            :


            Comment

            • Steve Gerrard

              #7
              Re: Returning an array from a function


              "Jim SUTTON" <jimSpamMeNot@b radleysutton.ne t> wrote in message
              news:LMbgb.2073 $ee6.1077@news-binary.blueyond er.co.uk...[color=blue]
              > I am sure I have had this problem before and can't remember what the fix
              > is...
              >
              > The documentation says that an array can be returned from a function
              > provided you do the following:-
              >
              > 1. Declare the function with an explicit return type - the examples suggest
              > it also needs the parentheses to denote a returned array thus:
              >
              > Private Function GetResultArray( RawResults as String, RequiredResultS etName
              > as String) as String()
              >
              > 2. Return the array (without parentheses) as part of the Exit Function
              > statement thus:
              >
              > Exit Function MyTempArray
              >
              > Why do I always get a syntax error on the Exit Function statement, with the
              > line compiler complaining it is expecting end of statement after Exit
              > Function?
              >
              > Any help appreciated...
              >
              > Regards
              >
              > --Jim.
              >
              >[/color]
              You need to exit with:
              FunctionName = MyTempArray
              Exit Function ' (or End Function)

              I never heard of putting a return value on the same line as Exit Function.


              Comment

              Working...