Results 1 to 6 of 6

Thread: Can a function return an Array? *Resolved*

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    7

    Can a function return an Array? *Resolved*

    Hello-
    I was wondering if a function can return an array. I have tried this, but could not get it to work. Does anyone else know how to do this?
    Thanks
    Last edited by netlias; Jan 11th, 2003 at 06:47 PM.

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385
    netlias,

    One way to do what you want is to pass the function a pointer to the array and have the function modify it.


    VB Code:
    1. Private sub Main
    2. dim tmpArray() as string
    3.  
    4.    PopulateArray(tmpArray)
    5.  
    6. end sub
    7.  
    8.  
    9. Private sub PopulateArray(byRef myArray() as string)
    10.  
    11.    redim myArray(100)
    12.  
    13.    myArray(0) = ....
    14.  
    15. end Sub


    Hope this helps...

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    7

    Thanks

    Thanks

  4. #4
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Or you can just return an array...
    VB Code:
    1. ' Function that returns an array:
    2. Private Function ReturnArray(nNumElements As Integer) As String()
    3.     Dim asTest() As String
    4.     Dim nCounter As Integer
    5.    
    6.     ReDim asTest(nNumElements)
    7.    
    8.     For nCounter = 0 To nNumElements - 1
    9.         asTest(nCounter) = "Element #" & nCounter + 1
    10.     Next
    11.    
    12.     ReturnArray = asTest
    13. End Function
    14.  
    15. ' Usage example:
    16. Private Sub TestSub()
    17.     Dim asMyStrArray() As String
    18.    
    19.     asMyStrArray = ReturnArray(5)
    20.    
    21.     MsgBox "UBound of asMyStrArray is: " & UBound(asMyStrArray)
    22. End Sub
    ~seaweed

  5. #5
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    with vb however, you needn't even include a parameter for the size of the array, as vb include ubound and lbound functions....unlike c++, that stupid language....[inaudible anger at c++]

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    119
    Originally posted by jmiller
    with vb however, you needn't even include a parameter for the size of the array, as vb include ubound and lbound functions....unlike c++, that stupid language....[inaudible anger at c++]
    lol
    agree agree

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