i am trying to pass and array to a function in a module so i can calculate the average with multiple entries and its not keeping the other entries only the lat entry. in the function i tried different way to see if i can get the private array and it doesnt work. any idea how to get the private array into the function for multiple entries any ideas

``````Module Module1
Private nBath(9) As Integer
Private nPeople(9) As Integer
Private nIncome(9) As Integer
Private nHouse(9) As Integer

Public Sub ProcessHouse(ByVal nHouse As Integer, ByVal nBath As Integer, ByVal nPeople As Integer, ByVal nIncome As Double, ByRef lstHouse As ListBox, ByRef txtAvgBath As TextBox, ByRef txtAvgPeople As TextBox, ByRef txtAvgIncome As TextBox)

lstHouse.Items.Add("House: " & nHouse & "     People: " & nPeople & "     Baths: " & nBath & "     Income: \$" & nIncome)

txtAvgBath.Text = CalAvgBath(nHouse, nBath, lstHouse, txtAvgBath)
'txtAvgBath.Text = CalAvgBath(nHouse, nBath, lstHouse)

End Sub

Private Function CalAvgBath(ByRef nHouse As Integer, ByRef nBath As Integer, ByRef lstHouse As ListBox, ByRef txtAvgBath As TextBox)

Dim sumBath() As Integer
Dim avgBath As Integer

ReDim Preserve sumBath(nBath)

CalAvgBath = avgBath

'1st way
For i = 0 To sumBath.GetUpperBound(0)
If i = sumBath(i) Then
avgBath = sumBath(i)
Else
sumBath(i) += nBath
avgBath = sumBath(i) / nHouse
End If
Next
txtAvgBath.Text = avgBath

''2nd way
'For i As Integer = 0 To sumBath.GetUpperBound(0)
'    If i = sumBath(i) Then
'        avgBath = sumBath(i)
'    Else
'        avgBath = sumBath(i) / nHouse
'    End If
'Next
'txtAvgBath.Text = avgBath

'3rd way
'For i As Integer = 0 To sumBath.GetUpperBound(0)
'    'If i = nBath Then
'    'avgBath = sumBath(i) / nHouse
'    'End If
'Next
'avgBath = sumBath(0) / nHouse
'Return avgBath

End Function

End Module
``````

HI,

I don't think the issue is passing the arrays, I think it is what you are doing with them...

Your nBath, nPeople, nHouse etc are all defined as single dimensioned integer arrays but for example, in your Sub routine ProcessHouse you seam to treat them as as single integer value:
`lstHouse.Items.Add("House: " & nHouse & " People: " & nPeople & " Baths: " & nBath & " Income: \$" & nIncome)`

``````for i =0 to 9
lstHouse.Items.Add("House: " & nHouse(i) & "     People: " & nPeople(i) & "     Baths: " & nBath(i) & "     Income: \$" & nIncome(i))
next
``````

Even then why not just use a single multi dimensional array?

``````dim Details(3,9) as integer
'...

for i = 0 to ubound(Details,2)-1
nHouse = Details(0,i)
nBath = Details(1,i)
nPeople = Details(2,i)
nIncome = Details(3,i)
next
``````

Also in your CalAvgBath function you have the line `ReDim Preserve sumBath(nBath)` nBath is an array of integers not an integer...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.