calculating the average of a total

  • 14 years ago
    Hello.
    I have 6 NumericUpDown fields that take values assigned by the user. There six of them. When each has been given a value, I want to calculate the average. I have been able to do this, but a logic error has manifested itself : I'm just adding the new values onto the total, without removing the previous value.
    So, if the first six values are all 1:
    1
    1
    1
    1
    1
    1,
    then the average will be 1.
    However when the values are changed
    2
    2
    2
    2
    2
    2. The average, instead of being 2, reads 3.

    Here is the code so far:

    dblAverage = intTotal / 6
          lblAverage.Text = Convert.ToString(dblAverage)

    Private Sub week1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles week1.ValueChanged

          intTotal += Convert.ToInt32(week1.Value)
       End Sub

       Private Sub week2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles week2.ValueChanged
          intTotal += Convert.ToInt32(week2.Value)
       End Sub

       Private Sub week3_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles week3.ValueChanged
          intTotal += Convert.ToInt32(week3.Value)
       End Sub

       Private Sub week4_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles week4.ValueChanged
          intTotal += Convert.ToInt32(week4.Value)
       End Sub

       Private Sub week5_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles week5.ValueChanged
          intTotal += Convert.ToInt32(week5.Value)
       End Sub

       Private Sub week6_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles week6.ValueChanged
          intTotal += Convert.ToInt32(week6.Value)
       End Sub

















































  • 14 years ago

     

    Put a watch on intTotal and you will see it is being "double dipped", i.e. when the numericUpDown1 is set to 1, intTotal is 1.  When you click on the nad again, thus it is 2, intTotal = 1 + 2, i.e. 3.

    You could tidy the calculation up by pointing all the nad ValueChanged events to the same handler:

     

    Private Sub nad1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nad1.ValueChanged, nad6.ValueChanged, nad5.ValueChanged, nad4.ValueChanged, nad3.ValueChanged, nad2.ValueChanged

    Dim total As Integer

    Dim average As Double

    total = nad1.Value + nad2.Value + nad3.Value + nad4.Value + nad5.Value + nad6.Value

    average = total / 6

    TextBox1.Text=

    String.Format("{0}", average)

    End Sub

    HTH

  • 14 years ago

    thanks for the reply. I can see what I was doing wrong, I can see how I was just adding on and not resetting the total. by pointing all the nad ValueChanged events under the same handler ... this is simply to "tidy" the code or is it more than that ? anyway thanks for responding
    DA


Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Never trust a programmer in a suit.” - Anonymous