DelphiScript - Using Arrays

Applies to TestComplete 14.10, last modified on June 5, 2019

Like other scripting languages, DelphiScript supports one- and multi-dimensional arrays. Arrays can be defined statically in the var section or created dynamically using the CreateVariantArray, CreateVariantArray2 or CreateVariantArray3 functions. Arrays can be passed to any routine as parameters. For example:

DelphiScript

procedure Test (p: OleVariant); // p is an array
begin
  Log.Message(p[0,0]);
  Log.Message(p[1,0]);
  Log.Message(p[0,1]);
  Log.Message(p[1,1]);
end;
 
procedure Test1;
var
  p : array [0..1, 0..1];
begin
  // Initializes array elements
  p[0,0] := '00000';
  p[1,0] := '11111';
  p[0,1] := 'aaaaa';
  p[1,1] := 'bbbbb';
  Test(p); // passes the array as parameter
end;

Note that arrays in DelphiScript cannot be initialized statically. For instance, the following code raises an error:

DelphiScript

var
  I : array[0..1] of Integer = (3,4) // <-- Error!!!
 

To solve the problem, initialize the array elements in your code. There are several ways to do this:

  • Initializing each element individually:

    DelphiScript

    var a : array [0..3] of string;
    begin
      a[0] := 'Element 0';
      a[1] := 'Element 1';
      a[2] := 'Element 2';
      a[3] := 'Element 3';
      ...
    end;
  • Initializing elements in a loop:

    DelphiScript

    var a : array [0..3] of string;
    begin
      for i := 0 to 3 do
        a[i] := 'Element ' + IntToStr(i);
      ...
    end;
  • Initializing all elements at once:

    DelphiScript

    var a : array [0..3] of string;
    begin
      a := ['Element 0', 'Element 1', 'Element 2', 'Element 3'];
      ...
    end;

See Also

DelphiScript Reference

Highlight search results