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.

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.