Example of basic Arithmetic Operation using Params Array in VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 77
Public Class ParamArithmeticOperation

Private Shared Sub Main()

'calling function

PerformArithmeticOperation(200, 100, "+")
'perform addition
PerformArithmeticOperation(200, 100, "-")
'perform subtraction
PerformArithmeticOperation(200, 100, "*")
'perform multiplication
PerformArithmeticOperation(200, 100, "/")
'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
Case Else

Console.WriteLine("No such operation available")
End Select
Console.WriteLine(result)
End Sub
End Class

Comments or Responses

Login to post response