Evaluates an expression in one simple step.
ucEval(Expression [, tHandle])
ucEvalStr(Expression [, ExprType] [, tHandle])
ucEval is a convenient one-step procedure for evaluating an expression. It is adequate for general situations where speed is not of utmost importance, such as when the expression is not recalculated many times in a loop. It is similar to calling ucParse, ucEvaluate, returning a value, and immediately releasing the expression from the definition space using ucReleaseItem.
Required. Expression is an argument you pass as a string, such as "5 ^ 2 + 3", the result of which you are interested in. An expression may also consist of multiple statements / expressions separated by a semi-colon (;). In the case of multiple statements / expressions, the value of the expression following the last semi-colon is the one that is returned.
Optional. This argument is typically not needed, since ucEvalStr selects an appropriate data type for the expression if this argument is omitted. However, if necessary, you can select a type. For instance, ucEvalStr("10/3") returns 3.333..., while ucEvalStr("10/3", ucLong) converts the expression to a Long integer, and returns 3. Both ExprType and tHandle represent item handles. uCalc can distinguish which is which, so either can be used as the second or third argument. So ucEvalStr("1+1", ucLong) and ucEvalStr("1+1", 0, ucLong) are equivalent. And you can use ucEvalStr("1+1", tHandle) if you want to specify a thread handle and not an expression type.
Optional. This represents the handle of a thread. By restricting an expression to a given thread, the expression will recognize only symbols that were defined in that thread (and its parent thread(s)). So you might have a variable x in one thread with one value, and variable x in another thread with a different value. Each expression will evaluate differently based on the value assigned to x in the thread you are evaluating in. See Thread Handling.
This example asks the user for an expression. A message box returns the result of evaluating the expression with ucEval. The MsgBox in VB accepts numeric as well as string values. In other compilers the following example might work only with ucEvalStr.
UserExpression$ = InputBox("Enter an expression", "Example", "5+4*10")
MsgBox ucEval(UserExpression$) ' Returns 45
Example 2: Evaluating miscellaneous expressions with ucEvalStr
This example asks the user for an expression. A message box returns the result of evaluating the expression with ucEvalStr. The default expression, 6+4*5/2, returns an ordinary numeric value, which is 16. The expressions below show that in addition to ordinary numeric values, ucEvalStr can also return a string, an error message, or special numeric values such as NaN (Note: 0 / 0 will return NaN if you are using the default FPU setting. If you toggled the FPU setting for uc_FPU_Mask_InvalidOp, it would return an invalid operation error message).
5 * 10 returns 50
"Hello " + "World" returns "Hello World"
1 + 2 / returns a "syntax error" message
0 / 0 returns NaN
UserExpression = InputBox("uCalc demo", "Enter an expression", "6+4*5/2");
// Create a TextBox named textBox1 on your form first.
// Then enter an expression such as: 6+4*5/2
UserExpression := InputBox('uCalc demo', 'Enter an expression', '6+4*5/2');
UserExpression$ = InputBox$("Enter an expression",, "6+4*5/2")
Visual Basic (classic)
UserExpression$ = InputBox("Enter an expression",, "6+4*5/2")
Visual Basic .NET
Dim UserExpression As String
UserExpression = InputBox("Enter an expression", , "6+4*5/2")
// This example is for a project designed as a Console Application
printf("Enter an expression (such as 6+4*5/2): ");
Example 3: Using threads with ucEval
This example demonstrates how the same expression can be evaluated differently in two different threads.
Dim Thread_A As Long, Thread_B As Long
Thread_A = ucNewThread()
ucDefineVariable "n = 50", 0, Thread_A
ucDefineFunction "f(x) = x ^ 2", 0, Thread_A
Thread_B = ucNewThread()
ucDefineVariable "n = 1000", 0, Thread_B
ucDefineFunction "f(x) = x + 1", 0, Thread_B
Print ucEval("f(5) + n", Thread_A) ' Returns 75
Print ucEval("f(5) + n", Thread_B) ' Returns 1006
New or enhanced in version 3.0
· Nothing related to this topic.
· See What’s New.
New or enhanced in version 2.96
New or enhanced in version 2.9+