Execute(SubName: String; Params: Array): Variant;
SubName - name of a method necessary to execute.
Params - parameters necessary to execute.
The Execute method executes a block of code on Fore and returns the result of execution.
Sub UserProc;
Var
Mb: IMetabase;
CodeBlock: IMetabaseCodeBlock;
Result: Variant;
Begin
Mb := MetabaseClass.Active;
CodeBlock := Mb.CreateCodeBlock;
CodeBlock.References := "MathFin";
CodeBlock.Text :=
"Function Test(x, y: Integer): Integer;" + #10 +
"Begin" + #10 +
" Return Math.RandBetweenI(x, y);" + #10 +
"End Function Test;";
If CodeBlock.Valid Then
Result := CodeBlock.Execute("Test", 1, 10);
Else
Debug.WriteLine("While code compiling an error appeared.");
Debug.WriteLine("Mistake text: " + CodeBlock.ErrorMessage);
Debug.WriteLine("Line: " + CodeBlock.Line.ToString);
Debug.WriteLine("Position: " + CodeBlock.Position.ToString);
End If;
Dispose CodeBlock;
End Sub UserProc;
On executing the example an object that executes a block of code on Fore is created. An executed code is assigned and it checks on syntax. If the code does not have mistakes, it is executed. Execution is performed with specified parameters values. The result of execution is in the Result variable. If the code has errors, the text of error and location of its appearing is displayed into development environment console. To execute the code the MathFin platform assembly is connected.
Executing the example requires a .NET form with the Button1 button and the TextBox component named TextBox1.
Private Sub button1_Click(sender: System.Object; e: System.EventArgs);
Var
Mb: IMetabase;
CodeBlock: IMetabaseCodeBlock;
Result: Object;
MyList: System.Collections.Generic.List <String> = New System.Collections.Generic.List <String>();
Begin
Mb := Self.Metabase;
CodeBlock := Mb.CreateCodeBlock();
CodeBlock.References := "MathFin";
CodeBlock.Text :=
"Function Test(x, y: Integer): Integer;" + char.ConvertFromUtf32(10) +
"Begin" + char.ConvertFromUtf32(10) +
" Return Math.RandBetweenI(x, y);" + char.ConvertFromUtf32(10) +
"End sub Test;";
If CodeBlock.Valid Then
Result := CodeBlock.Execute("Test", 1, 10);
Else
MyList.Add("While code compiling an error appeared.");
MyList.Add("Error text: " + CodeBlock.ErrorMessage);
MyList.Add("Line: " + CodeBlock.Line.ToString());
MyList.Add("Position: " + CodeBlock.Position.ToString());
TextBox1.Lines := MyList.ToArray();
End If;
End Sub;
After executing this example while pressing the button an object that executes code of block on Fore is created. An executed code is assigned and it checks on syntax. If the code does not have mistakes it is executed. Execution is performed with specified parameters values. The result of execution is in the Result variable. If the code has mistakes the text of a mistake and location of its appearing is displayed into the TextBox1 component. To execute the code the MathFin platform assembly is connected.
See also: