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