CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Help with array

  1. #1
    Join Date
    Dec 2016

    Help with array

    I am trying to find the matching year of the array after I find the maximum value of rainfall from the text document. My problem is with the sub procedure MostRainfallYearProcedure(). It is just sort of a mess. The only thing it does is find the max amount of rainfall from the list. I have never posted to a forum before and I don't know what is too much information to give, so pardon if this is overdoing it. Thank you. It should read "Year 2006 was the most rainfall for 2000-2017"
     'class variables for array
        Private _intRainfallArray As Integer = 17
        Private _strRainfallYear(_intRainfallArray) As String
        Private _decRainfallInInches(_intRainfallArray) As Decimal
        Private Sub RainfallStatistics_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            'The RainfallStatistics load event reads the text file and 
            'fills the Combobox so the user can select a year to display
            'the rainfall in inches for that year.  
            'Initialize the StreamReader and declare variables. 
            Dim objReader As IO.StreamReader
            Dim strLocationAndNameOfFile As String = "C:\Users\Marcus\Documents\rain.txt"
            Dim intCount As Integer = 0
            Dim intFill As Integer
            Dim strFileError As String = "This file is not available. Restart when the file is available"
            'Verify that the file exists
            If IO.File.Exists(strLocationAndNameOfFile) Then
                objReader = IO.File.OpenText(strLocationAndNameOfFile)
                'loop to read the file line by line 
                Do While objReader.Peek <> -1
                    _strRainfallYear(intCount) = objReader.ReadLine()
                    _decRainfallInInches(intCount) = Convert.ToDecimal(objReader.ReadLine)
                    intCount += 1
                'Combobox object is filled with rainfall years
                For intFill = 0 To (_strRainfallYear.Length - 1)
                MsgBox(strFileError, , "Error")
            End If
        End Sub
        Private Sub cboChooseYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cboChooseYear.SelectedIndexChanged
            'The Combobox event handler displays message 
            Dim intSelectedItemId As Integer
            If Me.cboChooseYear.SelectedIndex >= 0 Then
                intSelectedItemId = cboChooseYear.SelectedIndex
                lblTotalRainfallMessage.Text = ""
                lblTotalRainfallMessage.Visible = True
                lblTotalRainfallMessage.Text = "The total rainfall in " & _strRainfallYear(intSelectedItemId) & " is " & _decRainfallInInches(intSelectedItemId).ToString
            End If
        End Sub
        Private Sub btnDisplayStatistics_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDisplayStatistics.Click
            'Activates sub procedures to calculate average and display year of most rainfall
        End Sub
        Private Sub RainfallAverageProcedure()
            'procedure to calculate average rainfall
            Dim intAdd As Integer
            Dim decAddRainfallAmounts As Decimal
            Dim decAverageRainfallAmount As Decimal
            For intAdd = 0 To (_decRainfallInInches.Length - 1)
                decAddRainfallAmounts += _decRainfallInInches(intAdd)
                decAverageRainfallAmount = decAddRainfallAmounts / _intRainfallArray
            lblAverageRainfall.Visible = True
            lblAverageRainfall.Text = ""
            lblAverageRainfall.Text = "hello " & decAverageRainfallAmount.ToString("F1")
        End Sub
        Private Sub MostRainfallYearProcedure()
            'procedure to display year with most rainfall
            Dim strMostRainfall As String
            Dim decRow As Decimal
            decRow = _decRainfallInInches.Max()
            lblMostRain.Visible = True
            lblMostRain.Text = ""
            lblMostRain.Text = "Hello " & decRow
    Last edited by DataMiser; December 15th, 2016 at 11:16 PM.

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008

    Re: Help with array

    You mean something like this?
    decRow = _decRainfallInInches.Max()
    lblMostRain.Text = "Hello " & decRow_strRainfallYear(decRow).ToString
    btw there is no need to set the labeltext to "" before assigning it something else. Just more key strokes and slightly slower. Better to delete that line.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)