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
average(i)
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.

5
Contributors
6
Replies
19
Views
9 Years
Discussion Span
Last Post by Reverend Jim

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

Console.WriteLine(i)
Dim j() As String = Split(i, ",")
average(j)
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)

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

``````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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.