Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-01

property of an array

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jeremy Cowgar

I am trying to make this work:

type
     StringArray : array of String;

... later in code ...

procedure SetColumnNames(names : StringArray);
property ColumnNames : StringArray write SetColumnNames;

... later in code ...

procedure TMyObject.SetColumnNames(names : StringArray);
begin
     ... code to set column names ...
end;

... later in code ...

myObj.ColumnNames := [ 'Id', 'Name', 'Age' ];

I get an error on the above line:

Error: Ordinal expression expected

Any thoughts? Thanks!

Jeremy

Reply by email

Joost van der Sluis
to Jeremy Cowgar

Op zaterdag 03-01-2009 om 19:04 uur [tijdzone -0500], schreef Jeremy Cowgar:

myObj.ColumnNames := [ 'Id', 'Name', 'Age' ]; Error: Ordinal expression expected

A list of values between square brackets are a set, not an array.

You'll have to do:

  AnArray[0] := 'Id';
  anArray[1] := 'Name';
  anArray[2] := 'Age';
  myObj.ColumnNames := AnArray;

Or create a constant array and use that.

Joost.

Reply by email

Jeremy Cowgar
to Joost van der Sluis
Joost van der Sluis wrote 17 lines
Op zaterdag 03-01-2009 om 19:04 uur [tijdzone -0500], schreef Jeremy Cowgar: > myObj.ColumnNames := [ 'Id', 'Name', 'Age' ]; > Error: Ordinal expression expected A list of values between square brackets are a set, not an array. You'll have to do: AnArray[0] := 'Id'; anArray[1] := 'Name'; anArray[2] := 'Age'; myObj.ColumnNames := AnArray; Or create a constant array and use that.

My original definition is:

   procedure SetColumnNames(names : StringArray);
   property ColumnNames : StringArray write SetColumnNames;

Why does it work when I call it by the procedure?

myObj.SetColumnNames(['Id', 'Name', 'Age']);

but not:

myObj.ColumnNames := ['Id', 'Name', 'Age'];

Jeremy

Reply by email

dmitry boyarintsev
to Jeremy Cowgar
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

Reply by email

Jeremy Cowgar
to dmitry boyarintsev
dmitry boyarintsev wrote 11 lines
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); ???

Hm, you are right. When I was able to do SetColumnNames([...]); was before I changed to a defined type, trying to get around this problem.

Jeremy

Reply by email