tags:

views:

139

answers:

1

Hi All,

Sure, even I can do this...

var
  testarray : array of string;

setlength(testarray, 5);

but if I want to get clever and have a procedure into which I pass the array by referance like this I cant do it!

procedure DoStuffWithArray(var passedarray : array of string);
begin
  setlength(passedarray, 5);
end;

compiler says 'incompatible types' on the single line of code in my proc.

I can do other stuff on the array like set set element values if i do a setlength before i call the proc, but I cannot do the setlength in my proc, which is what I want to do.

Any help much appreciated, thanks all.

+9  A: 

Array types as parameters need to have a name. So:

type TStringArray = array of string;
procedure DoStuffWithArray(var passedarray: TStringArray);

Then it would work.

But if you need a dynamically-sized group of strings, you'd probably find a TStringList easier to use anyway.

Mason Wheeler
Thank you very much Mason :)
csharpdefector
@Csharpdefetor - If you like Mason's answer (which is a good one) then you ought to accept it. You will get rep bonus too.
Jim McKeeth
This is due to historical quirk in Delphi's syntax, in that it uses the same syntax for open array parameters. See http://stackoverflow.com/questions/2112729/biggest-delphi-nitpicks/2113650#2113650
Gerry