ParamArray means to allow a subrotine to take a variable no. of arguments.
I m just sending the arguments suppose i send 1,2,3,4,5
I want just the addition of these numbers.

Sub main()
Dim i As Integer
i = Console.ReadLine()
average(i)
Console.ReadLine()
End Sub

Sub average(ByVal ParamArray b() As Integer)
Dim total As Integer = 0
For i As Integer = LBound(b) To UBound(b)
total = total + b(i)
Next

Console.WriteLine(total.ToString)
End Sub

I m not understanding what is wrong in the program,y the o/p is not coming which is intended.
plz go thru it & do reply back.

Dani AI

Generated

Good question, . A ParamArray lets you call a method with a variable number of values, e.g. Sum(1,2,3), or by passing an Integer() you already built. In your first attempt you read a whole line like 1,2,3,4,5 into a single Integer, which cannot work. Either pass the numbers directly (as showed) or read the line as text, split it, convert to integers, then call the ParamArray method. Also note your procedure printed a sum, not an average. And yes, iterating directly over the array (as hinted) is simpler than using LBound/UBound. was right that the called method receives an array; the key is how you supply it.

Here is a compact pattern you can drop in and test. It handles both literal calls and typed input in the console without repeating code:

Module Demo
    Function Sum(ParamArray values() As Integer) As Long
        Dim total As Long = 0
        For Each n As Integer In values
            total += n
        Next
        Return total
    End Function

    Function Mean(ParamArray values() As Integer) As Double
        If values Is Nothing OrElse values.Length = 0 Then Return Double.NaN
        Return CDbl(Sum(values)) / values.Length
    End Function

    Sub Main()
        Dim raw As String = Console.ReadLine()
        Dim parts = raw.Split({","c, " "c}, StringSplitOptions.RemoveEmptyEntries)
        Dim nums = Array.ConvertAll(parts, Function(s) Integer.Parse(s))
        Console.WriteLine(Sum(nums))
        Console.WriteLine(Mean(nums))
    End Sub
End Module

Tips for future readers:

  • ParamArray must be the last parameter and is always passed ByVal.
  • Prefer For Each over LBound/UBound for clarity.
  • Validate input and handle the empty case to avoid divide-by-zero.
  • Use Long for totals to reduce overflow risk on large inputs.
  • Turn Option Strict On to catch implicit conversions early.

Recommended Answers

All 6 Replies

I have never used a ParamArray but looking at your code your average is asking for an array of integers and you are passing it only an integer.
Dim i as Integer
If you are passing i then define it as:
Dim i() as integer

Even when i m passing i() as integer,program is not working correctly...

Try this:

Sub main()
        Dim i As String

        i = Console.ReadLine()
        Console.WriteLine(i)
        Dim j() As String = Split(i, ",")
        average(j)
        Console.ReadLine()
    End Sub

    Sub average(ByVal ParamArray b() As String)
        Dim total As Integer = 0
        For i As Integer = LBound(b) To UBound(b)
            total = total + CInt(b(i))
        Next

        Console.WriteLine(total.ToString / (UBound(b) + 1))
    End Sub

This is assuming your input is => 1,2,3
If your input is +> 1 2 3 then change the , in split to a space.

' To understand ParamArray more clearly

Imports System

Class test

Public Shared Sub main()

average(1, 2, 3, 4, 5)
average(4, 5, 6)

Console.ReadLine()
End Sub

Shared Sub average(ByVal ParamArray b() As Integer)
Dim total As Integer = 0
For i As Integer = LBound(b) To UBound(b)
total = total + b(i)
Next

Console.WriteLine(total.ToString)
End Sub
End Class

ME PROGRAM HAS ERRORS PLEASE HELP ME TO REMOVE THOSE ERROR

Public Sub DisplayVals(ByVal ParamArray intVals( ) As Integer)
    Dim i As Integer
    For Each i In intVals()
        Console.WriteLine("DisplayVals {0}", i)
    Next i
End Sub  

Use

For Each i In intVals

No parentheses. And please do not post in threads that are inactive. This one has been dead for more than four years.

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.