0

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
2
Contributors
1
Reply
11
Views
3 Years
Discussion Span
Last Post by G_Waddell
0

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)
Instead of something like:

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...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.