Recent

Author Topic: Weird error  (Read 691 times)

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
Weird error
« on: April 13, 2026, 03:39:03 am »
I defined a function:

function AddDirToDataPackage(var fh: integer; SrcDir: string; var FileNameLists: array of string): boolean;
...
Setlength(FileNameLists, c + 1);
...

Compilation error indicates at line "Setlength(FileNameLists, c + 1); ": "Type mismatch".

If i do like this:
function AddDirToDataPackage(var fh: integer; SrcDir: string; var FileNameLists: array of string): boolean;
var
    aa: array of string;
begin
    Setlength(aa, c + 1);
...

Setlength(aa, c + 1); is ok in Compilation.

hoping for guidance.

xiyi0616

dseligo

  • Hero Member
  • *****
  • Posts: 1683
Re: Weird error
« Reply #1 on: April 13, 2026, 03:44:12 am »
Do it like this:
Code: Pascal  [Select][+][-]
  1. function AddDirToDataPackage(var fh: integer; SrcDir: string; var FileNameLists: TStringArray): boolean;
  2. ...
  3. Setlength(FileNameLists, c + 1);

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
Re: Weird error
« Reply #2 on: April 13, 2026, 03:47:04 am »
OK  :)

n7800

  • Hero Member
  • *****
  • Posts: 689
  • Lazarus IDE contributor
    • GitLab profile
Re: Weird error
« Reply #3 on: April 14, 2026, 02:31:23 am »
If you're curious about the cause of the error, you can read about the differences between dynamic arrays and open arrays in the documentation.

Thaddy

  • Hero Member
  • *****
  • Posts: 19129
  • Glad to be alive.
Re: Weird error
« Reply #4 on: April 14, 2026, 06:13:09 am »
As indicated above It is likely sufficient to replace the open array parameter by TStringArray - defined in sysutils - instead of the open array of string.

In trunk there is also the copy command to copy an open array to a dynamic array: https://wiki.freepascal.org/FPC_New_Features_Trunk#Copy_supports_Open_Array_parameters

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses sysutils;  
  3.  
  4.   function copyarray(const values:array of string):TStringArray;  
  5.   begin
  6.     Result := copy(values,0,length(values));
  7.   end;
  8.  
  9. var
  10.   a:TStringArray;  
  11.   s:string;
  12. begin
  13.   a:= copyarray(['1','2','3'] );
  14.   for s in a do writeln(s);
  15. end.
Of course you can also do the copy to a local variable of type TStringArray;
There is no need for setlength. This just illustrates the technique.

You use a var parameter for your open array: you can't change the length in that case, you must use TstringArray in that case (or TStringDynArray from types, which is the same) as parameter.
You can change the value of the individual elements of an open array parameter, though.

What is also an option is to pass a TStringArray as an open array. This is automatic. E.g.:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. uses sysutils;  
  3.  
  4.   procedure test(var values:array of string);
  5.   var s:string;
  6.   begin
  7.     if length(values) > 0 then values[0] := 'test';
  8.     for s in values do writeln(s);
  9.   end;
  10.  
  11. var
  12.   a:TStringArray = ['1','2','3'];  
  13. begin
  14.   test(a);
  15.   setlength(a,4);
  16.   a[3] :='test2';
  17.   test(a);
  18. end.
This likely works in 3.2.X too, but not tested.



« Last Edit: April 14, 2026, 07:10:39 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

xiyi0616

  • Jr. Member
  • **
  • Posts: 64
Re: Weird error
« Reply #5 on: April 14, 2026, 07:10:18 am »
Thank you, n7800 , I read it.
Thank you, Thaddy, for explaining everything in such detail.
« Last Edit: April 14, 2026, 07:28:40 am by xiyi0616 »

 

TinyPortal © 2005-2018