Binding a uCalc array to a host array

ID: 1466

				
					using uCalcSoftware;

var uc = new uCalc();
double[] myHostArray = new double[] { 5, 10, 15, 20 };

// Pin the array
var myHostArrayHandle = System.Runtime.InteropServices.GCHandle.Alloc(myHostArray, System.Runtime.InteropServices.GCHandleType.Pinned);

// Bind the uCalc variable to the pinned memory address
uc.DefineVariable("MyArray[]", myHostArrayHandle.AddrOfPinnedObject());

// The uCalc array values come from the host array
Console.WriteLine(uc.Eval("MyArray[0]"));
Console.WriteLine(uc.Eval("MyArray[1]"));
Console.WriteLine(uc.Eval("MyArray[2]"));
Console.WriteLine(uc.Eval("MyArray[3]"));
Console.WriteLine("");

// Changing the uCalc array updates the pinned array
uc.Eval("MyArray[0] = 10; MyArray[1] = 20; MyArray[2] = 30; MyArray[3] = 40; ");

// The changes the uCalc array are reflected in the host array
Console.WriteLine(myHostArray[0]);
Console.WriteLine(myHostArray[1]);
Console.WriteLine(myHostArray[2]);
Console.WriteLine(myHostArray[3]);
Console.WriteLine("");

// One more round trip
myHostArray[0] = 10;
myHostArray[1] = 11;
myHostArray[2] = 12;
myHostArray[3] = 13;

Console.WriteLine(uc.Eval("MyArray[0]"));
Console.WriteLine(uc.Eval("MyArray[1]"));
Console.WriteLine(uc.Eval("MyArray[2]"));
Console.WriteLine(uc.Eval("MyArray[3]"));

// Don't forget to free the handle when you are completely done with the parser instance
myHostArrayHandle.Free();
				
			
5
10
15
20

10
20
30
40

10
11
12
13
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   double myHostArray[] = { 5, 10, 15, 20 };
   uc.DefineVariable("MyArray[]", &myHostArray);
   // The uCalc array values come from the host array
   cout << uc.Eval("MyArray[0]") << endl;
   cout << uc.Eval("MyArray[1]") << endl;
   cout << uc.Eval("MyArray[2]") << endl;
   cout << uc.Eval("MyArray[3]") << endl;
   cout << "" << endl;

   // Changing the uCalc array updates the pinned array
   uc.Eval("MyArray[0] = 10; MyArray[1] = 20; MyArray[2] = 30; MyArray[3] = 40; ");

   // The changes the uCalc array are reflected in the host array
   cout << myHostArray[0] << endl;
   cout << myHostArray[1] << endl;
   cout << myHostArray[2] << endl;
   cout << myHostArray[3] << endl;
   cout << "" << endl;

   // One more round trip
   myHostArray[0] = 10;
   myHostArray[1] = 11;
   myHostArray[2] = 12;
   myHostArray[3] = 13;

   cout << uc.Eval("MyArray[0]") << endl;
   cout << uc.Eval("MyArray[1]") << endl;
   cout << uc.Eval("MyArray[2]") << endl;
   cout << uc.Eval("MyArray[3]") << endl;
}
				
			
5
10
15
20

10
20
30
40

10
11
12
13
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim myHostArray() As Double = { 5, 10, 15, 20 }
      
      '// Pin the array
      Dim myHostArrayHandle = System.Runtime.InteropServices.GCHandle.Alloc(myHostArray, System.Runtime.InteropServices.GCHandleType.Pinned)
      
      '// Bind the uCalc variable to the pinned memory address
      uc.DefineVariable("MyArray[]", myHostArrayHandle.AddrOfPinnedObject())
      
      '// The uCalc array values come from the host array
      Console.WriteLine(uc.Eval("MyArray[0]"))
      Console.WriteLine(uc.Eval("MyArray[1]"))
      Console.WriteLine(uc.Eval("MyArray[2]"))
      Console.WriteLine(uc.Eval("MyArray[3]"))
      Console.WriteLine("")
      
      '// Changing the uCalc array updates the pinned array
      uc.Eval("MyArray[0] = 10; MyArray[1] = 20; MyArray[2] = 30; MyArray[3] = 40; ")
      
      '// The changes the uCalc array are reflected in the host array
      Console.WriteLine(myHostArray(0))
      Console.WriteLine(myHostArray(1))
      Console.WriteLine(myHostArray(2))
      Console.WriteLine(myHostArray(3))
      Console.WriteLine("")
      
      '// One more round trip
      myHostArray(0) = 10
      myHostArray(1) = 11
      myHostArray(2) = 12
      myHostArray(3) = 13
      
      Console.WriteLine(uc.Eval("MyArray[0]"))
      Console.WriteLine(uc.Eval("MyArray[1]"))
      Console.WriteLine(uc.Eval("MyArray[2]"))
      Console.WriteLine(uc.Eval("MyArray[3]"))
      
      '// Don't forget to free the handle when you are completely done with the parser instance
      myHostArrayHandle.Free()
   End Sub
End Module
				
			
5
10
15
20

10
20
30
40

10
11
12
13