IMetabaseCodeBlock.Execute

Syntax

Execute(SubName: String; Params: Array): Variant;

Execute(SubName: String; paramarray Params: Array of Object): Object;

Parameters

SubName. Name of the method to be executed.

Params. Parameters required for execution.

Description

The Execute method executes code block on Fore and returns execution result.

Example

Executing the example requires that the repository contains an assembly with the ASM_CORE identifier. The assembly contains a module where the GenerateNumber function is implemented.

Add a link to the Metabase system assembly.

Sub UserProc;
Var
    Mb: IMetabase;
    CodeBlock: IMetabaseCodeBlock;
    Result: Variant;
Begin
    Mb := MetabaseClass.Active;
    CodeBlock := Mb.CreateCodeBlock;
    CodeBlock.References := 
"MathFin;ASM_CORE";
    CodeBlock.Text :=
        
"Function Test(x, y: Integer): Integer;" + #13 + #10 +
        
"Var" + #13 + #10 +
        
"   n: Integer;" + #13 + #10 +
        
"Begin" + #13 + #10 +
        
"   n := GenerateNumber;" + #13 + #10 +
        
"   If (n>x) And (n<y) Then" + #13 + #10 +
        
"      Return Math.Int((y-x)/n);" + #13 + #10 +
        
"   Else" + #13 + #10 +
        
"      Return Math.Int(n/(y-x));" + #13 + #10 +
        
"   End If" + #13 + #10 +
        
"End Function Test;";
    
If CodeBlock.Valid Then
        Result := CodeBlock.Execute(
"Test"110);
        Debug.WriteLine(Result);
    
Else
        Debug.WriteLine(
"While code compilation an error appeared.");
        Debug.WriteLine(
"Error text: " + CodeBlock.ErrorMessage);
        Debug.WriteLine(
"String: " + CodeBlock.Line.ToString);
        Debug.WriteLine(
"Position: " + CodeBlock.Position.ToString);
    
End If;
    
Dispose CodeBlock;
End Sub UserProc;

Imports Prognoz.Platform.Interop.Metabase;

Public Shared Sub Main(Params: StartParams);
Var
    Mb: IMetabase;
    CodeBlock: IMetabaseCodeBlock;
    Result: Object;
Begin
    Mb := Params.Metabase;
    CodeBlock := Mb.CreateCodeBlock();
    CodeBlock.References := 
"MathFin;ASM_CORE";
    CodeBlock.Text :=
        
"Function Test(x, y: Integer): Integer;" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"Var" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"   n: Integer;" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"Begin" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"   n := GenerateNumber;" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"   If (n>x) And (n<y) Then" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"      Return Math.Int((y-x)/n);" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"   Else" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"      Return Math.Int(n/(y-x));" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"   End If" + char.ConvertFromUtf32 (13)+ char.ConvertFromUtf32 (10)  +
        
"End Function Test;";
    
If CodeBlock.Valid Then
        Result := CodeBlock.Execute(
"Test"110);
        System.Diagnostics.Debug.WriteLine(Result);
    
Else
        System.Diagnostics.Debug.WriteLine(
"While code compilation error appeared.");
        System.Diagnostics.Debug.WriteLine(
"Error text: " + CodeBlock.ErrorMessage);
        System.Diagnostics.Debug.WriteLine(
"String: " + CodeBlock.Line.ToString());
        System.Diagnostics.Debug.WriteLine(
"Position: " + CodeBlock.Position.ToString());
    
End If;
End Sub;

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 execution result will be contained in the Result variable and displayed to the development environment console. 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 system assembly and the ASM_CORE custom assembly are connected.

See also:

IMetabaseCodeBlock