Purpose of the program: The Windows application opens a text file with the populations of the 10 largest cities in the united states. The user selects a city and displays the population for next five years with a projected 3% growth per year. A menu selection also can show the top 10 cities and their present population on a second windows form object.

I am having trouble getting the projected growth to show in the list box. Here is my code

Option Strict On

Public Class frmProjectedPopulation

    'Class level variables
    Private _intYears As Integer = 5
    Public Shared _intSizeOfArray As Integer = 9
    Public Shared _strCities(_intSizeOfArray) As String
    Public Shared _intPopulation(_intSizeOfArray) As Integer

    Private Sub frmProjectedPopulation_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ' The frmProjectedPopulation load event reast the cities text file and fills the ComboBox object with the Cities

        ' Initialize an instance of the StreamReader object and declare variables 
        Dim objReader As IO.StreamReader
        Dim strLocationAndNameOfFile As String = "G:\CPC240\Chapter 09\Chapter 09\CaseProjects\cities.txt"
        Dim intCount As Integer = 0
        Dim intFill As Integer
        Dim strFileError As String = " The file is not available. Restart when the file is available."
        ' Verify the file exists
        If IO.File.Exists(strLocationAndNameOfFile) Then
            objReader = IO.File.OpenText(strLocationAndNameOfFile)
            ' Read the file line by line until the file is completed
            Do While objReader.Peek <> -1
                _strCities(intCount) = objReader.ReadLine()
                _intPopulation(intCount) = CInt(objReader.ReadLine())
                intCount += 1
            ' The ComboBox object is filled with the Names of the Cities
            For intFill = 0 To (_strCities.Length - 1)
            MsgBox(strFileError, , "Error")

        End If
    End Sub

    Private Sub btnComputeExpectedGrowth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeExpectedGrowth.Click
        ' This button click event calls the Growth sub procedures
        ' Declare variables
        Dim intSelectedCity As Integer
        Dim strMissingSelection As String = "Missing Selection"
        Dim strSelectCityError As String = "Select a City"

        ' If a ComboBox object is selected, call the Growth procedure
        If Me.cboCityName.SelectedIndex >= 0 Then
            intSelectedCity = cboCityName.SelectedIndex
            MsgBox(strSelectCityError, , strMissingSelection)

        End If
    End Sub

    Private Sub FutureGrowth(ByVal intCity As Integer)
        ' This sub procedure diplays the current population and also the population for the next 5 years for the city selected
        ' Declare variables
        Dim decFutureGrowth As Decimal
        Dim decPopulationTotal As Decimal
        Dim strSelectCity As String = " The present population is "

        lblPopulation.Text = strSelectCity & _intPopulation(_intSizeOfArray)
        lstExpectedGrowth.Text = CStr(decFutureGrowth)

        decFutureGrowth = CDec(_intPopulation(intCity) * 0.03)

        ' The loop repeats for 5 years
        For decFutureGrowth = 1 To _intYears
            'Accumulates the future population
            decPopulationTotal += decFutureGrowth
            ' Displays the future population amounts
            decPopulationTotal -= decFutureGrowth

    End Sub
    Private Sub MakeObjectsVisible()
        ' This procedure displays the objects after a city is selected
        lblPopulation.Visible = True
        lblExpectedGrowth.Visible = True
        lstExpectedGrowth.Visible = True
        ' Previous data removed

    End Sub

    Private Sub mnuDisplayPresentPopulation_Click(sender As System.Object, e As System.EventArgs) Handles mnuDisplayPresentPopulation.Click
        ' The mnuDisplayPresentPopulation click event creates an instance of the frmCurrentLargestCities
        Dim frmSecond As New frmCurrentLargestCities

        ' Hide this form and show the Display Inventory form

    End Sub

    Private Sub mnuClear_Click(sender As System.Object, e As System.EventArgs) Handles mnuClear.Click
        ' The mnuClear click event clears and resets the form
        cboCityName.SelectedIndex = -1
        lblExpectedGrowth.Visible = False
        lblPopulation.Visible = False
        lstExpectedGrowth.Visible = False

    End Sub

    Private Sub mnuExit_Click(sender As System.Object, e As System.EventArgs) Handles mnuExit.Click
        ' The mnuExit click event closes the application

    End Sub
End Class

Try replacing your loop header where you populate the listbox with

For intFill = 0 To 9

You may be reading too many lines into your arrays. You can verify by putting a breakpoint at line 32 and checking the array bounds.

This article has been dead for over six months. Start a new discussion instead.