Working With the Class

Parent Previous Next

Working With the Class.

Here is an example of how to use the C# class:



//C# code:


       private void Test()

       {

           //While testing we use redirection - loading the bytecode from a CUF.

           string bytecode = @"FILE=c:\test\project1.cuf";

           

           Int32 vm = DLL.VMCreate(bytecode);


           DLL.VMCellSetInteger(vm, 1, -1, 987654321);

           

           Int32 result = DLL.VMExecute(vm, 1, 2, 3);

           

           DLL.VMFree(vm);

       }



The Test() function manages all the VM related work:


These steps are all very similar to how one would write the equivalent source code in SCRAMBLECODE, to create a function which could create, execute and free another VM in bytecode. Notice how we can use the static C# functions without having to create an instance of the DLL class - e.g. simply writing DLL.VMFree - which is very similar to writing @VMFree in SCRAMBLECODE.


Using CallBack.

Now we expand the code to include a callback function:



//C# code:


       private void Test()

       {

           //While testing we use redirection - loading the bytecode from a CUF.

           string bytecode = @"FILE=c:\test\project1.cuf";

           

           Int32 vm = DLL.VMCreate(bytecode);


           DLL.VMCellSetInteger(vm, 1, -1, 987654321);


           DLL.VMSetCallback(vm, MyCallbackFunction);

           

           Int32 result = DLL.VMExecute(vm, 1, 2, 3);

           

           DLL.VMFree(vm);

       }



       private static Int32 MyCallbackFunction(Int32 x, Int32 y, Int32 z)

       {

           return x - y - z;

       }



Please notice that the callback function is static.