Simple calculation

Is there a way to solve simple calculations in the form (with calculation rules):
2 + (5-3) * 2

A string with natural numbers and basic operators.

Sure: CDbl will give you the value of a result as a double; CStr will change an integer or a double to a string.

So, something like this in a pushbutton’s Action event will show you.

MsgBox(CStr(CDbl(“2 + (5-3) * 2”)))

Perfect! Thank you!

That solution won’t work, and the MsgBox will give you the answer “2” for that particular string (it’s returning the first number it finds). You’ll need to write a routine to run through the calculation.

Your best bet would probably be to use a XojoScript object.

  1. Add a XojoScript object to your project (e.g. XojoScript1).
  2. Add the following to the XojoScript1.Print event:
MsgBox msg // msg is the answer 
  1. Add the following code to an action event of a button
  Dim expression As String = "2 + (5-3) * 2"
  
  XojoScript1.Source = "Print(Str(" + expression + "))"
  XojoScript1.Run

Just set the expression variable equal to the expression that you need to evaluate. The msg parameter in the XojoScript1.Print event can be converted to a double with:

MyAnswer = Val(msg)

Here is a link to a nice solution

http://www.xdevzone.com/2008/01/creating-an-eval-function-using-rbscript/

Yes, I just found it out. I will have a look at the scripting. Thanks…

(hmm. can’t remove the “this pos answered my question” mark…)

Go up to your original post and point at the “answer” there. You will see the “X” that will let you remove it.

An example of this is included with Xojo:

Examples/Advanced/XojoScript/Evaluator