dimanche 19 mai 2019

Invoke arithmetic operator dynamically

Powershell has arithmetic operators like addition (+), substraction (-) and bitwise and (-band). I'm making a simple calculator program, and I want to dynamically perform arithmetic calculations based on the user input, without needing to write a lot of if-else statements. Is there a way to dynamically invoke powershell operators? E.g. if $method=="plus" do "6+6".

I know there is Invoke-Expression, but that doesn't really operate on the operator alone (you also need to supply the operands in the expression string). Is there some way to define the operator as a variable? E.g. $method="-band", $result=6 $method 6;





Aucun commentaire:

Enregistrer un commentaire