Results 1 to 4 of 4

Thread: How to pass array as return value?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    Is it possible to have a function return an array as it's return value?

    Well actually, I guess it is possible to return an array because it seem to be returning the array, but I can't figure out how to use it in the calling sub.

    When it the function returns the array, and I try to assign that array to another array, it won't let me.. So I guess the real question is, how do you use an array returned from a function?

    Any help or examples would be appreciated..

    dan

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Question CODE

    If I could see the code, I could be of more help.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Nevermind... I found the answer.. Both arrays have to be dynamic..

    Thanks,

    Dan

  4. #4
    Addicted Member
    Join Date
    Sep 2000
    Posts
    138
    Perhaps here is THE code:
    Code:
        '
        '--------------------------------------------------
        ' Demonstration of a Sub returning an array
        ' Simulating the Split function in VB 6
        ' Create a new project with a large textbox
        ' Set the MultiLine property of the textbox to True
        ' strMyArray() is the returned array
        '--------------------------------------------------
    Option Explicit
    
    Private Sub Form_Load()
        Dim strMyString As String
        Dim i As Long
        Dim strMyArray() As String
        
        strMyString = "This is a trial." & vbCrLf & "It is a good trial." & vbCrLf & "I like it very much" _
            & vbCrLf & "Do you like it?" & vbCrLf & "Yes, I do." & vbCrLf & "We all do, too."
        
        RetArray strMyString, vbCrLf, strMyArray()
        For i = 0 To UBound(strMyArray)
            Text1.Text = Text1.Text & strMyArray(i) & vbCrLf
        Next
        
    End Sub
    
    Private Sub RetArray(ByVal InString As String, ByVal Separator As String, RetArr() As String)
        '
        '--------------------------------------------------
        ' Sub returning an array
        '--------------------------------------------------
        Dim i As Long
        Dim j As Long
        Dim k As Long
        i = 0
        j = 1
        Do While j < Len(InString)
            k = j
            Do While j < Len(InString)
                If Mid$(InString, j, Len(Separator)) = Separator Then
                    ReDim Preserve RetArr(i)
                    RetArr(i) = Mid$(InString, k, j - k)
                    j = j + 1
                    Exit Do
                End If
                j = j + 1
            Loop
            i = i + 1
        Loop
        
    End Sub

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