[fpc-pascal] property of an array

dmitry boyarintsev skalogryz.lists at gmail.com
Sun Jan 4 22:07:36 CET 2009


1)
type
   StringArray : array of String;

defines a StringArray type to be dynamic array.
are you sure, that you have SetColumnNames defined as
procedure SetColumnNames(names : StringArray);
rather than:
procedure SetColumnNames(names : array of string);
???

because if you have
procedure SetColumnNames(names : StringArray);
you cannot pass array in the following way
SetColumnNames(['1','2','3']);

because: ['1','2','3'] is an open array, while SetColumnNames requires
a dynamic array to be passed.

btw, there's no way to define open-array type. Open arrays can be
passed only as a parameters.

2)
if you have dynamic array type declared you CAN use it as a property

type
   StringArray : array of String;

...
  fNames : StringArray;
  procedure SetColumnNames(ANames: StringArray);

  property ColumnNames : StringArray read fNames write SetColumnNames

.. So if you want to assign a value to the ColumnNames property, you
can do it, only by generating a dynamic array first:
// the function generates a StringArray from Open String Array!
function MakeStringArray(const Strs: array of string): StringArray;
var
  i : integer;
begin
  SetLength(Result, length(strs));
  for i := 0 to length(strs) - 1 do
    Result[i] := strs[i];
end;
...
// compiles and works nicely and effectevely
// glory to FreePascal (and originally delphi 7 introducing open arrays)
myobj.Names := MakeStringArray(['1','2','3','4','5']);

3) You must understand the difference between dynamic array types and
open-arrays parameters



More information about the fpc-pascal mailing list