A practical example of retrieving an expression's parent uCalc instance to dynamically add a formatting rule before evaluation.

ID: 605

				
					using uCalcSoftware;

var uc = new uCalc();
// Practical (Real World)
// Parse an expression
var myExpr = uc.Parse("5 + 4");
Console.WriteLine($"Initial evaluation: {myExpr.Evaluate()}");

// Retrieve the parent uCalc instance from the expression object
var parentUc = myExpr.uCalc;

// Use the parent instance to add a new formatting rule
parentUc.Format("Result = 'Answer = ' + Result");

// The new format is now active for this expression's context
Console.WriteLine($"Formatted evaluation: {myExpr.EvaluateStr()}");
				
			
Initial evaluation: 9
Formatted evaluation: Answer = 9
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Practical (Real World)
   // Parse an expression
   auto myExpr = uc.Parse("5 + 4");
   cout << "Initial evaluation: " << myExpr.Evaluate() << endl;

   // Retrieve the parent uCalc instance from the expression object
   auto parentUc = myExpr.uCalc();

   // Use the parent instance to add a new formatting rule
   parentUc.Format("Result = 'Answer = ' + Result");

   // The new format is now active for this expression's context
   cout << "Formatted evaluation: " << myExpr.EvaluateStr() << endl;
}
				
			
Initial evaluation: 9
Formatted evaluation: Answer = 9
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Practical (Real World)
      '// Parse an expression
      Dim myExpr = uc.Parse("5 + 4")
      Console.WriteLine($"Initial evaluation: {myExpr.Evaluate()}")
      
      '// Retrieve the parent uCalc instance from the expression object
      Dim parentUc = myExpr.uCalc
      
      '// Use the parent instance to add a new formatting rule
      parentUc.Format("Result = 'Answer = ' + Result")
      
      '// The new format is now active for this expression's context
      Console.WriteLine($"Formatted evaluation: {myExpr.EvaluateStr()}")
   End Sub
End Module
				
			
Initial evaluation: 9
Formatted evaluation: Answer = 9