Solved

# Calculate average with array and class

Posted on 2011-05-07

I am new and am having difficulties. I have a text box a user can enter up to 10 numbers, a button to enter the numbers and another to average the numbers with the results in a 2nd text box. Now an easy way to do this would be as follows.

Public Class frmAverage

Dim count As Integer = 0

Dim total As Integer = 0

Private Sub btnRecordNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecordNumber.Click

'Add the numbers to the total

total += CInt(txtNumber.Text)

count += 1

txtNumber.Clear()

txtNumber.Focus()

End Sub

Private Sub btnCalculateAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateAverage.Click

'Average the numbers to the total

txtAverage.Text = CStr(total / count)

End Sub

End Class 'frmAverage

However, I am required to use a class named Statistics and have an AddNumber method that stores numbers into an array one at a time. The class should have a Count property that keeps track of the number of numbers stored and a method called Average that returns the average of the numbers. Therefore, I have come up with this but I think I am getting really lost in what I am doing. I think I just need someone to point me in the correct direction. Any advice would be appreciated.

Public Class frmAverage

Dim numbr(50) As Statistics

Dim count As Double

Dim lastNumAdded As Double 'position in array the last number addes

Private Sub btnRecordNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecordNumber.Click

'Add the numbers and count

number += CInt(txtNumber.Text)

count += 1

txtNumber.Clear()

txtNumber.Focus()

End Sub

Private Sub btnCalculateAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateAverage.Click

'Average the numbers

'num.Count(CDbl(txtNumber.Text))

txtAverage.Text = number / 2

txtNumber.Clear()

txtNumber.Focus()

End Sub

End Class 'frmAverage

Class Statistics

Private m_addNumber As String

Public Property number() As String

Public WriteOnly Property addNumber() As Double

Set(ByVal value As Double)

m_addNumber = number

End Set

End Property

End Class