Hello all,
I am new to VB and need to make a program that does the following:

Write a VB.Net program that allows users to enter a series of numbers
(separated by a comma) in a text box called series. When the users clicks the command button called Average, the program extracts the number one by one from the series and calculates the average, and outputs the average onto a textbox called Result.

I understand how to get it to do the average, but how can I make the program count the numbers that I input and then divided by the sum? Also, how would i get it started.

Any help will be appreciated.

Dani AI

Generated

A reliable, beginner-friendly way is: extract numeric tokens from the textbox, parse each token with TryParse (so invalid entries don't crash the app), keep a running sum and a counter, then divide sum by count. This handles empty items (extra commas), whitespace, and lets you decide whether to skip or report invalid tokens. Note: comma is ambiguous — it’s the list separator here, but some locales use comma as the decimal separator. Normalize decimals (or require a dot) to avoid surprises.

' Example for the Average button (place at top of file: Imports System.Text.RegularExpressions, Imports System.Globalization)
Private Sub Average_Click(sender As Object, e As EventArgs) Handles Average.Click
    Dim text As String = series.Text
    Dim sum As Double = 0
    Dim count As Integer = 0

    ' Find integers or decimals (allows optional +/-, dots or commas as decimal sep)
    Dim rx As New Regex("[-+]?\d+([.,]\d+)?")
    For Each m As Match In rx.Matches(text)
        Dim token As String = m.Value.Replace(","c, "."c) ' normalize comma -> dot
        Dim value As Double
        If Double.TryParse(token, NumberStyles.Float, CultureInfo.InvariantCulture, value) Then
            sum += value
            count += 1
        End If
    Next

    Result.Text = If(count > 0, (sum / count).ToString("G"), "No valid numbers")
End Sub

Notes and quick fixes tied to earlier replies:

  • ’s compact LINQ approach is elegant for experienced users, but it requires the LINQ extension methods (Imports System.Linq) and correct Split usage; the "ToList is not a member of System.Array" error usually means the LINQ import or overload choice is missing.
  • ’s List-related errors come from confusing control collections vs. generic lists: a generic list needs a type parameter (e.g., New List(Of Double)), while a ListBox control uses ListBox.Items.Add(item).
  • Edge cases: trailing commas, non-numeric tokens, or mixed separators — decide whether to skip invalid tokens or show an error. Normalize decimal separators when users may enter commas as decimals.

Recommended Answers

All 2 Replies

I wouldn't suggest doing this as a 1-liner (as shown below) but to give you an idea:

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    Dim s As String = "1,2,3,4,5,6,7,8,9,10,11,12"
    Dim avg As Double = s.Split(",").ToList().ConvertAll(New Converter(Of String, Integer)(AddressOf Convert.ToInt32)).Average()
    MessageBox.Show(avg)
  End Sub

When running your program there was a build array saying that Error 'ToList' is not a member of 'System.Array'.

Also, I haven't learned array yet in class so I want to avoid if possible.

This is what i have:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim s As Double
        Dim resultstring As String
        Dim anumericvalue As Double

        List.items.insert(",")
        s = 0
        For i = 0 To List.items.count - 1
            s = s + List.items.item(i)
        Next i
        resultstring = Str(s / List.items.count)
        result1.Text = "average is: " & resultstring
    End Sub

But the build errors i have are all dealing with List.
IE Error Too few type arguments to 'System.Collections.Generic.List(Of T)'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.