EvalStrMethod

Applies to:uCalc Transform
Class:uCalc
Parses and evaluates an expression of any data type in one step, returning the result as a string
Syntax
EvalStr(Expression)
Parameters
Expression
String
Expression to be evaluated
Returns
Type: String
Parses and evaluates an expression of any data type in one step, returning the result as a string
Remarks
This is similar to Eval, however, it can return non-numeric results, such as strings.
Example 1: Evaluating expressions

Console.WriteLine(uc.EvalStr("1 + 1")) ' Returns 2
Console.WriteLine(uc.EvalStr("UCase('Hello ' + 'world!')")) ' Returns "HELLO WORLD!"      
Console.WriteLine(uc.EvalStr("(3+5*#i)^2")) ' Returns "-16+30i" 

          

Console.WriteLine(uc.EvalStr("1 + 1")); // Returns 2;
Console.WriteLine(uc.EvalStr("UCase('Hello ' + 'world!')")); // Returns "HELLO WORLD!";      
Console.WriteLine(uc.EvalStr("(3+5*#i)^2")); // Returns "-16+30i"; 

          

      WriteLn(uc.EvalStr('1 + 1')); // Returns 2;
      WriteLn(uc.EvalStr('UCase("Hello " + "world!")')); // Returns 'HELLO WORLD!';      
      WriteLn(uc.EvalStr('(3+5*#i)^2')); // Returns '-16+30i'; 

          

cout << uc.EvalStr("1 + 1") << endl; // Returns 2;
cout << uc.EvalStr("UCase('Hello ' + 'world!')") << endl; // Returns "HELLO WORLD!";      
cout << uc.EvalStr("(3+5*#i)^2") << endl; // Returns "-16+30i"; 

          

Console::WriteLine(uc.EvalStr("1 + 1")); // Returns 2;
Console::WriteLine(uc.EvalStr("UCase('Hello ' + 'world!')")); // Returns "HELLO WORLD!";      
Console::WriteLine(uc.EvalStr("(3+5*#i)^2")); // Returns "-16+30i"; 

          
Example 2: Finding the precedence level of an operator

Console.Write("The precedence level of the 'And' operator is: ")
Console.WriteLine(uc.GetItemOf("And").Precedence()) ' Returns 30
uc.DefineOperator("{a} && {b} As Bool = a And b", uc.GetItemOf("And").Precedence())
Console.WriteLine(uc.EvalStr("1 > 0 && 2 > 0")) ' Returns "true"
Console.WriteLine(uc.EvalStr("1 > 2 && 2 > 0")) ' Returns "false"

          

Console.Write("The precedence level of the 'And' operator is: ");
Console.WriteLine(uc.GetItemOf("And").Precedence()); // Returns 30;
uc.DefineOperator("{a} && {b} As Bool = a And b", uc.GetItemOf("And").Precedence());
Console.WriteLine(uc.EvalStr("1 > 0 && 2 > 0")); // Returns "true";
Console.WriteLine(uc.EvalStr("1 > 2 && 2 > 0")); // Returns "false";

          

      Write('The precedence level of the "And" operator is: ');
      WriteLn(uc.GetItemOf('And').Precedence()); // Returns 30;
      uc.DefineOperator('{a} && {b} As Bool = a And b', uc.GetItemOf('And').Precedence());
      WriteLn(uc.EvalStr('1 > 0 && 2 > 0')); // Returns 'true';
      WriteLn(uc.EvalStr('1 > 2 && 2 > 0')); // Returns 'false';

          

cout << "The precedence level of the 'And' operator is: ";
cout << uc.GetItemOf("And").Precedence() << endl; // Returns 30;
uc.DefineOperator("{a} && {b} As Bool = a And b", uc.GetItemOf("And").Precedence());
cout << uc.EvalStr("1 > 0 && 2 > 0") << endl; // Returns "true";
cout << uc.EvalStr("1 > 2 && 2 > 0") << endl; // Returns "false";

          

Console::Write("The precedence level of the 'And' operator is: ");
Console::WriteLine(uc.GetItemOf("And").Precedence()); // Returns 30;
uc.DefineOperator("{a} && {b} As Bool = a And b", uc.GetItemOf("And").Precedence());
Console::WriteLine(uc.EvalStr("1 > 0 && 2 > 0")); // Returns "true";
Console::WriteLine(uc.EvalStr("1 > 2 && 2 > 0")); // Returns "false";

          
Example 3: Setting variables of any data type

Dim Int32Var = uc.DefineVariable("Int32Var As Int32")
Dim ByteVar = uc.DefineVariable("ByteVar As Byte")
Dim StrVar = uc.DefineVariable("StrVar As String")
Dim SngVar = uc.DefineVariable("SngVar As Single")

Int32Var.SetVariableValueAny("4.25"): ' Will be converted to integer
ByteVar.SetVariableValueAny("-1"): ' Will be converted to unsigned byte
StrVar.SetVariableValueAny("'Test'")
SngVar.SetVariableValueAny("1.25")

Console.WriteLine(uc.Eval("Int32Var")) ' Returns 4
Console.WriteLine(uc.Eval("ByteVar")) ' Returns 255
Console.WriteLine(uc.EvalStr("StrVar")) ' Returns "Test"
Console.WriteLine(uc.EvalStr("SngVar")) ' Returns 1.25

          

var Int32Var = uc.DefineVariable("Int32Var As Int32");
var ByteVar = uc.DefineVariable("ByteVar As Byte");
var StrVar = uc.DefineVariable("StrVar As String");
var SngVar = uc.DefineVariable("SngVar As Single");

Int32Var.SetVariableValueAny("4.25"); // Will be converted to integer
ByteVar.SetVariableValueAny("-1");    // Will be converted to unsigned byte
StrVar.SetVariableValueAny("'Test'");
SngVar.SetVariableValueAny("1.25");

Console.WriteLine(uc.Eval("Int32Var")); // Returns 4;
Console.WriteLine(uc.Eval("ByteVar")); // Returns 255;
Console.WriteLine(uc.EvalStr("StrVar")); // Returns "Test";
Console.WriteLine(uc.EvalStr("SngVar")); // Returns 1.25;

          

//var Int32Var = uc.DefineVariable('Int32Var As Int32');
//var ByteVar = uc.DefineVariable('ByteVar As Byte');
//var StrVar = uc.DefineVariable('StrVar As String');
//var SngVar = uc.DefineVariable('SngVar As Single');

      Int32Var.SetVariableValueAny('4.25'); // Will be converted to integer
      ByteVar.SetVariableValueAny('-1');    // Will be converted to unsigned byte
      StrVar.SetVariableValueAny('"Test"');
      SngVar.SetVariableValueAny('1.25');

      WriteLn(uc.Eval('Int32Var')); // Returns 4;
      WriteLn(uc.Eval('ByteVar')); // Returns 255;
      WriteLn(uc.EvalStr('StrVar')); // Returns 'Test';
      WriteLn(uc.EvalStr('SngVar')); // Returns 1.25;

          

auto Int32Var = uc.DefineVariable("Int32Var As Int32");
auto ByteVar = uc.DefineVariable("ByteVar As Byte");
auto StrVar = uc.DefineVariable("StrVar As String");
auto SngVar = uc.DefineVariable("SngVar As Single");

Int32Var.SetVariableValueAny("4.25"); // Will be converted to integer
ByteVar.SetVariableValueAny("-1");    // Will be converted to unsigned byte
StrVar.SetVariableValueAny("'Test'");
SngVar.SetVariableValueAny("1.25");

cout << uc.Eval("Int32Var") << endl; // Returns 4;
cout << uc.Eval("ByteVar") << endl; // Returns 255;
cout << uc.EvalStr("StrVar") << endl; // Returns "Test";
cout << uc.EvalStr("SngVar") << endl; // Returns 1.25;

          

auto Int32Var = uc.DefineVariable("Int32Var As Int32");
auto ByteVar = uc.DefineVariable("ByteVar As Byte");
auto StrVar = uc.DefineVariable("StrVar As String");
auto SngVar = uc.DefineVariable("SngVar As Single");

Int32Var.SetVariableValueAny("4.25"); // Will be converted to integer
ByteVar.SetVariableValueAny("-1");    // Will be converted to unsigned byte
StrVar.SetVariableValueAny("'Test'");
SngVar.SetVariableValueAny("1.25");

Console::WriteLine(uc.Eval("Int32Var")); // Returns 4;
Console::WriteLine(uc.Eval("ByteVar")); // Returns 255;
Console::WriteLine(uc.EvalStr("StrVar")); // Returns "Test";
Console::WriteLine(uc.EvalStr("SngVar")); // Returns 1.25;

          
DLL import code
<DllImport(uCalcDLL, CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl, EntryPoint:="EvalStr")> _

Private Function EvalStr__(ByVal uCalcHandle As IntPtr,ByVal Expression As String) As IntPtr
End Function
            
[DllImport(uCalcDLL, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl, EntryPoint="EvalStr")]

protected static extern  IntPtr EvalStr_(IntPtr uCalcHandle, string Expression);
            
{DLLImport}function EvalStr__(uCalcHandle: System.Pointer;Expression: AnsiString):  PAnsiChar; cdecl; external uCalcDLL name 'EvalStr';

            
typedef const char * (* __EvalStr)(void *uCalcHandle, CONSTCHAR Expression ); 

            
[DllImport(uCalcLib, CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl, EntryPoint = "EvalStr")]

static STR_RETURN EvalStr_(void *  uCalcHandle, MARSHALSTR Expression);
            
See also
Prev | Next