Rabskatran Rabskatran - 2 months ago 10
Vb.net Question

Function or Method with unknown number of parameters

Is there a way to create a method with unknown number of parameters?

And if it this the case :


  • How is it possible to get access to them within this method?

  • Do they have to be from the same type?


Answer

Yes & yes.

it is possible, and all of them must be the same type, if you need to pass various types use object datatype instead, and then unbox them within function. use ParamArray:

' Accept variable number of arguments 
Function Sum(ByVal ParamArray nums As Integer()) As Integer 
  Sum = 0  
  For Each i As Integer In nums 
    Sum += i 
  Next 
End Function   ' Or use Return statement like C#

Dim total As Integer = Sum(4, 3, 2, 1)   ' returns 10

for more info see this