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.Cou nt
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.Ite m(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.Ite m(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.
My function code is:
Public Function Array_lstCells()
Dim a As Integer = lstCells.SelectedItems.Cou
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.Ite
'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.Ite
'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.
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_lstCel ls(0, 0) & vbCrLf & arr_lstCells(0, 1) & vbCrLf & arr_lstCells(0, 2) & vbCrLf & arr_lstCells(0, 3))
End Sub
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
Array_lstCells()
MessageBox.Show(arr_lstCel
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok that now works. Thank you for your help.
Public Function Array_lstCells() As Object(,)
And add a return statement before End Function:
Return arr_lstCells
End Function