[fpc-pascal] add a string to existed open array

Sven Barth pascaldragon at googlemail.com
Tue Jan 31 10:19:46 CET 2012


Am 31.01.2012 09:55, schrieb ik:
> Hello,
>
> I wish to create something like this:
>
> function foo(a : String; b : array of const) : string;
> begin
>    Result := bar([a] + b);
> end;
>
> But this works only on Sets.
> The thing is, that A must be before the rest of "b". and it is different
> then the b values, that's why I extract it to a different parameter.
>
> How can I add a to the list, and make it the first element ?

There is no easy way, only the following:

function foo(a: String; b: array of const): String;
var
   tmp: array of TVarRec;
   i: LongInt;
begin
   SetLength(tmp, Length(b) + 1);
   tmp[0].VType := vtAnsiString; // Note: Only if "foo" is in a unit 
with {$H+} set (or mode Delphi)
   tmp[0].VAnsiString := Pointer(a);
   for i := 0 to High(b) do
     tmp[i + 1] := b[i];
   Result := bar(tmp);
end;

Regards,
Sven



More information about the fpc-pascal mailing list