virtualmind virtualmind - 1 year ago 76 Question function to return an operator

i want the code to return an operator such as (+) or (-)
and then use that to perform a calculation. is that possible in


public function rOperator(params) as operator

if (..)
return +

return -
end if

end function


Msgbox(1 rOperator 2)

Answer Source

As already said before, that is not possible. What you can do is create a dictionary of Func(Of T) and then invoke those by passing in your operator:

Sub Main
    Dim operations As New Dictionary(Of String, Func(Of Double, Double, Double))()

    'Set up operations for addition, subtractions, multipication, and division
    operations.Add("+", Function(l, r) l + r)
    operations.Add("-", Function(l, r) l - r)
    operations.Add("*", Function(l, r) l * r)
    operations.Add("/", Function(l, r) l / r)

    Dim result As Double = operations("+")(5, 5)
    Dim result2 As Double = operations("*")(5, 5)


End Sub