Using Visual Basic 10, and I am trying to identify min/max statistics and display the results. My calculations are working, but the display is not (line 68 & 80). With the way the code is written now, I don't get the error but my results are displayed as "The minimum monthly rainfall was correct result ("g"). "g" should be the month that corresponds to the minimum monthly rainfall. The same issue occurs on the maximum display.

The line was initially written as lblMinMonthly.Text = ("The minimum monthly rainfall was " & intMinRainfall.ToString() & "(" & strMonths(Me.intCount) & ")") but received an index out of range exception error. Any suggestions on how to correct?

My second issue is that the btnClear_Click event clears my list box and labels (begins line 84), but the array does not reset itself. In order to enter a new set of monthly statistics I have to exit and rerun. Is there a simple fix?

Thanks

Public Class Form1
    'Class level declarations
    Const intMAX_SUBSCRIPT As Integer = 11              'Upper subscript
    Dim intRainfall(intMAX_SUBSCRIPT) As Integer        'Holds rainfall array
    Dim strMonths() As String = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
    Dim intCount As Integer                             'Loop Counter

    Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
        'Prepare use for data entry
        MessageBox.Show("Please enter rainfall by month as an integer beginning with January.")

        Do While intCount <= intMAX_SUBSCRIPT
            Try
                'Get monthly rainfall from user and store in an array
                intRainfall(intCount) = CInt(InputBox("Enter the rainfall for " & strMonths(intCount)))

                'Increment intCount
                intCount += 1

            Catch ex As Exception
                'Error message for invalid input
                MessageBox.Show("Enter monthly rainfall as in integer.")
            End Try
        Loop

        'Clear the list box of its current contents
        lstRainfall.Items.Clear()

        'Display the rainfall listbox header
        lstRainfall.Items.Add("Monthly Rainfall Input")
        lstRainfall.Items.Add("--------------------------------")

        'Display each months rainfall
        For Me.intCount = 0 To strMonths.Length - 1
            lstRainfall.Items.Add(intRainfall(intCount).ToString() & " for " & strMonths(intCount))
        Next
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim intTotalRainfall As Integer                     'Total annual rainfall
        Dim dblAverageRainfall As Double                    'Average annual rainfall
        Dim intMinRainfall As Integer                       'Mininum monthly rainfall
        Dim intMaxRainfall As Integer                       'Maximum monthly rainfall


        'Calculate and display the total annual rainfall
        For Me.intCount = 0 To (intRainfall.Length - 1)
            intTotalRainfall += intRainfall(intCount)
        Next

        lblTotalAnnual.Text = ("The total annual rainfall was " & intTotalRainfall.ToString())

        'Calculate and display the average annual rainfall
        dblAverageRainfall = intTotalRainfall / intRainfall.Length
        lblAveMonthly.Text = ("The average monthly rainfall was " & dblAverageRainfall.ToString())

        'Calculate and display the minimum  monthly rainfalls
        'Get first month's rainfall
        intMinRainfall = intRainfall(0)

        'Search for lowest monthly rainfall
        For Me.intCount = 1 To (intRainfall.Length - 1)
            If intRainfall(Me.intCount) < intMinRainfall Then
                intMinRainfall = intRainfall(Me.intCount)
            End If
        Next

        lblMinMonthly.Text = ("The minimum monthly rainfall was " & intMinRainfall.ToString() & " (" & strMonths.ToString(intCount) & ")")

        'Calculate and display the maximum monthly rainfalls
        'Get first month's rainfall
        intMaxRainfall = intRainfall(0)

        'Search for highest monthly rainfall
        For Me.intCount = 1 To (intRainfall.Length - 1)
            If intRainfall(Me.intCount) > intMaxRainfall Then
                intMaxRainfall = intRainfall(Me.intCount)
            End If
        Next
        lblMaxMonthly.Text = ("The maximum monthly rainfall was " & intMaxRainfall.ToString() & " (" & strMonths.ToString(intCount) & ")")

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clear the list box and statistics
        lstRainfall.Items.Clear()
        lblTotalAnnual.Text = String.Empty
        lblAveMonthly.Text = String.Empty
        lblMinMonthly.Text = String.Empty
        lblMaxMonthly.Text = String.Empty
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'Close the form
        Me.Close()
    End Sub
End Class

Edited 3 Years Ago by happygeek: moved

Don't do

strMonths.ToString(intCount)

ToString is evaluated first on strMonths resulting in "System.String[]". Because strMonths is already of type String (array), there is no need to apply ToString. Just use

strMonths.(intCount)

To clear an array you just loop over all the elements and set each one to zero such as

For i As Integer = 0 To Ubound(myArray)
    myArray(i) = 0
Next

Edited 3 Years Ago by Reverend Jim

My initial attempt was the strMonths(intCount), but I recieved an error IndexOutOfRangeException was unhandled stating the index was outside the boundarirs of the array. I understand what the message is communicating, but I don't understand why I'm getting it.

I know its not the correct solution, but to test I changed it to strMonths(intCount-1). The program ran and produced (December) for both the minimum and maximum.

Edited 3 Years Ago by SLMQC

You're accessing intCount outisde the for loop. Thus it will always point to 12, which will throw the IndexOutOfRangeException , cause your array is only indexed to 11. This is also why you always get December when you use intcount-1

Something like this should be closer to what you want:

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
    Dim intTotalRainfall As Integer 'Total annual rainfall
    Dim dblAverageRainfall As Double 'Average annual rainfall
    Dim intMinRainfall As Integer 'Mininum monthly rainfall
    Dim intMaxRainfall As Integer 'Maximum monthly rainfall
    Dim strMaxMonth, strMinMonth As String 'Months with the minimum and maximum rainfall
    'Calculate the rainfall statistics
    'Get first month's rainfall
    intMinRainfall = intRainfall(0)
    intMaxRainfall = intRainfall(0)
    For Me.intCount = 0 To (intRainfall.Length - 1)
        'Add up the monthly rainfall
        intTotalRainfall += intRainfall(intCount)
        'Search for lowest monthly rainfall
        If intRainfall(Me.intCount) < intMinRainfall Then
            intMinRainfall = intRainfall(Me.intCount)
            strMinMonth = strMonths(intCount)
        End If
        'Search for highest monthly rainfall
        If intRainfall(Me.intCount) > intMaxRainfall Then
            intMaxRainfall = intRainfall(Me.intCount)
            strMaxMonth = strMonths(intCount)
        End If
    Next
    'Display the total annual rainfall
    lblTotalAnnual.Text = ("The total annual rainfall was " & intTotalRainfall.ToString())
    'Calculate and display the average monthly rainfall
    dblAverageRainfall = intTotalRainfall / intRainfall.Length
    lblAveMonthly.Text = ("The average monthly rainfall was " & dblAverageRainfall.ToString())
    'Display the minimum monthly rainfal
    lblMinMonthly.Text = ("The minimum monthly rainfall was " & intMinRainfall.ToString() & " (" & strMinMonth & ")")
    'Display the maximum monthly rainfall
    lblMaxMonthly.Text = ("The maximum monthly rainfall was " & intMaxRainfall.ToString() & " (" & strMaxMonth & ")")
End Sub

Edited 3 Years Ago by tinstaafl

This question has already been answered. Start a new discussion instead.