It been a long time since i have coded ; mostly in Java. However, I am taking a class in vb.net and I am having trouble with the syntax and methods in vb. I have this goofy calculator project the part i am stumped on is taking a value from an input and adding it to an arraylist. it appears the every value entered is just over writing the same index and not adding to the array correctly. the object is to keep adding to the array until the calculate button is pressed and then total the sum in the array divide by the count and get an average.

Public Class AverageScoreCalculator

    Private Sub TestScoreBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub NumberTestBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub BestScoreBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged

    End Sub

    Private Sub AverageScoreBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged

    End Sub

    Private Sub Averagebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Totals As New List(Of Decimal)
        Dim BestScore As Decimal
        Dim Scores As Decimal
        Dim AverageScore As Decimal

        If IsNumeric(TextBox1.Text) AndAlso Val(TextBox1.Text) >= 0 AndAlso Val(TextBox1.Text) <= 100 Then
            Scores = Val(TextBox1.Text)

        Else : MessageBox.Show("Either a nonnumeric value or not a number between 0 and 100")
            TextBox1.Clear()
            TextBox1.Select()

        End If
        Totals.Add(Scores)
        AverageScore = Totals.Sum / Totals.Count

        If AverageScore > BestScore Then
            BestScore = AverageScore

        Else : BestScore = 0

        End If

        TextBox2.Text = Totals.Count
        TextBox3.Text = BestScore
        TextBox4.Text = AverageScore

    End Sub

    Private Sub ClearBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        Totals.Items.Clear()

    End Sub

    Private Sub ExitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub

    Private Sub BestScorelbl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

    End Sub

    Private Sub AverageScorelbl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click

    End Sub

    Private Sub TestScorelbl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub NumberTestlblGrades_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub

End Class

You have declared the list Totals local to the event handler Averagebtn_Click. Hence, each time when the button is clicked, a new list is created. Instead, just declare it in the scope of the class.

thanks I move the array class scope He says as he shakes more of the rust off.

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