Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Public Class frmAverage

Dim count As Integer = 0

Dim total As Integer = 0

Private Sub btnRecordNumber_Click(ByVa

'Add the numbers to the total

total += CInt(txtNumber.Text)

count += 1

txtNumber.Clear()

txtNumber.Focus()

End Sub

Private Sub 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(ByVa

'Add the numbers and count

number += CInt(txtNumber.Text)

count += 1

txtNumber.Clear()

txtNumber.Focus()

End Sub

Private Sub btnCalculateAverage_Click(

'Average the numbers

'num.Count(CDbl(txtNumber.

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

http://msdn.microsoft.com/en-us/library/ms973814.aspx#creatingclasses_topic3

you dont need the public keyword as all properties are public.

```
Class Statistics
Private m_currenttotal As double '<<<<< this counts all the added numbers
Private m_currentcount as long 'counts how many numbers have been added
WriteOnly Property addNumber() As Double
Set(ByVal value As Double)
m_currenttotal += value 'build the total
m_currentcount += 1 'increment the counter
End Set
End Property
ReadOnly Property Average() as double
Get
Return currenttotal / currentcount
End Get
End Property
End Class
```

AddNumber should be a method (sub or function) not a property.

If you can reason with your teacher/prof, use a List(Of Integer) instead of an array. It's type-safe and gives you a performance benefit since there's no boxing/unboxing. As an added benefit, you get Add() for free, and since it implements IEnumerable(Of T) there's an Average extension method baked in. (I don't know of any professionals that use arrays; generic collections, introduced in .NET 2.0, are far superior).

Dim numbr(50) As Statistics.... this should be "Private statistics As New Statistics". It doesn't need to be an array of Statistics -- the Statistics class will contain the array, and manage it through the AddNumber method. Which, btw, raises another reason generic collections such as List are nice -- you don't have to manage the allocation of memory based on the size.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27020761.html

```
Public Class StatList
Inherits List(Of Double)
Private m_Sum As Double
Public Overloads Sub Add(ByVal value As Double)
MyBase.Add(value)
m_Sum += value
End Sub
Public Overloads Sub AddRange(ByVal values() As Double)
MyBase.AddRange(values)
For i As Integer = 0 To values.Count - 1
m_Sum += values(i)
Next
End Sub
Public Overloads Sub Clear()
MyBase.Clear()
m_Sum = 0
End Sub
Public ReadOnly Property Average() As Double
Get
If MyBase.Count = 0 Then Return 0
Return m_Sum / MyBase.Count
End Get
End Property
End Class
```

Now you can use all benefits of List(of double)

Dim StList As New StatList

'AddRange allow add an array at once

StList.AddRange(New Double() {1.1, 2.22, 3.333, 4.4444, 5.55555})

MsgBox(StList.Average)

'Build in

MsgBox(StList.Min)

MsgBox(StList.Max)

'As well as Find,BinarySearch, contains etc,etc

```
Public Class frmAverage
Private Stats As New Statistics
Private Sub btnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecord.Click
Dim TheNumberEntered As Decimal
If Decimal.TryParse(txtNumber.Text, TheNumberEntered) Then
Stats.AddNumber(CInt(TheNumberEntered))
txtNumber.Text = Nothing
txtNumber.Focus()
Else
MessageBox.Show("Invalid Grade Entered")
End If
End Sub
Private Sub btnCalculateAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateAverage.Click
txtAnswer.Text = CStr(Stats.Average)
End Sub
End Class
Class Statistics
Private m_numbers(49) As Integer
Private m_count As Integer 'Count of numbers entered into array
Sub AddNumber(ByVal number As Integer)
'Adds a number to the statistics
m_count += 1
m_numbers(m_count) = number
End Sub
Public ReadOnly Property Count() As Integer
'Counter of numbers stored
Get
Return m_count
End Get
End Property
Function Average() As Double
'Computes the average
Dim sum As Integer = 0
For i As Integer = 1 To m_count
sum += m_numbers(i)
Next
Return sum / m_count
End Function
End Class 'Statistics
```

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

This way your form's code will look a little like:

Open in new window