How to pass variable number of arguments using Params in VB.net?

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 705
If we want to pass variable number of arguments from calling function to called function, we can use the Params Arrays
e.g.

Public Class ParamTest
Private Shared Sub Main()
'calling function
PerformArithmeticOperation(20, 10, "+")
'perform addition
PerformArithmeticOperation(20, 10, "-")
'perform subtraction
PerformArithmeticOperation(20, 10, "*")
'perform multiplication
PerformArithmeticOperation(20, 10, "/")
'perform division
End Sub

'called function
Public Shared Sub PerformArithmeticOperation(ParamArray items As Object())
Dim result = 0
Select Case Convert.ToString(items(2))
'items[2] is the operator(s) i.e. +,-,*,/
Case "+"C
result = Convert.ToInt32(items(0)) + Convert.ToInt32(items(1))
Exit Select
Case "-"C
result = Convert.ToInt32(items(0)) - Convert.ToInt32(items(1))
Exit Select
Case "*"C
result = Convert.ToInt32(items(0)) * Convert.ToInt32(items(1))
Exit Select
Case "/"C
result = Convert.ToInt32(items(0)) / Convert.ToInt32(items(1))
Exit Select

End Select
Console.WriteLine(result)
End Sub
End Class

Comments or Responses

Login to post response