Link to home
Start Free TrialLog in
Avatar of stewdaddy
stewdaddy

asked on

How do I pass an array from a function?

I wrote a function that breaks some data into an array.  Now I need to use that array in another procedure.  How do I pass the array out of the function, and how do I call it into my other procedure?  

My function code is:

   Public Function Array_lstCells()
        Dim a As Integer = lstCells.SelectedItems.Count
        Dim arr_lstCells(a, 3)
        Dim i As Integer = 0
        Dim sb As String = ""

        For Each sel As Object In lstCells.SelectedItems
            arr_lstCells(i, 0) = lstCells.SelectedItems.Item(i)
            'Assign project # based on txtProject
            arr_lstCells(i, 1) = txtProject.Text
            'Split out the path and project# from the data file
            Dim strSplitListItem As String() = lstCells.SelectedItems.Item(i).split("-")
            'Split out the cell and file type from the first split
            Dim strSplitListItem2 As String() = strSplitListItem(1).Split(".")
            'Assign the cell
            arr_lstCells(i, 2) = strSplitListItem2(0)
            'Assign the file type
            arr_lstCells(i, 3) = strSplitListItem2(1)

            i = i + 1
            sb = sb & sel.ToString() & vbCrLf
        Next
    End Function

I want to pass arr_lstCells out of the function.
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

Try changing the function declaration to:
Public Function Array_lstCells() As Object(,)

And add a return statement before End Function:

  Return arr_lstCells
End Function
Avatar of stewdaddy
stewdaddy

ASKER

And then how do I call it into the procedure?  I keep getting a "variable not declared" error for aar_lstCells.  My Procedure looks like this:

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Array_lstCells()
        MessageBox.Show(arr_lstCells(0, 0) & vbCrLf & arr_lstCells(0, 1) & vbCrLf & arr_lstCells(0, 2) & vbCrLf & arr_lstCells(0, 3))
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok that now works.  Thank you for your help.