0

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

PseudoCode

public function rOperator(params) as operator

if (..)
return +

else 
return -
end if

end function 

PseudoCode

Msgbox(1 rOperator 2)
1
  • No, this is not possible. The most simple way would be to make a function which calculates differently depending on your condition(s). I.e. DoMath(1, 2) Commented May 13, 2016 at 21:50

1 Answer 1

2

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)

    Console.WriteLine(result)
    Console.WriteLine(result2)

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.