Recent

Author Topic: Array return  (Read 1286 times)

Petrus Vorster

  • Full Member
  • ***
  • Posts: 241
Array return
« on: August 01, 2025, 12:40:56 pm »
Hi All

Is it possible to have a function that will return an array with e.g. 4 elements?
I have read somewhat on this, but i am getting a great deal of errors.

I need to send a string to a function, search that string in a database, then return an array with those findings.
The google searches have my head all mixed up by now.

I have never done this before.
Can someone please show me how you do that?

-Regards, Peter

RayoGlauco

  • Full Member
  • ***
  • Posts: 227
  • Beers: 1567
Re: Array return
« Reply #1 on: August 01, 2025, 12:55:40 pm »
I think you can solve the problem this way, using arrays, whether dynamic or not:

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   TMyArrayOfInteger = array of integer;
  4.  
  5.   TMyRecord = record
  6.     number: integer;
  7.     text: string[50];
  8.   end;
  9.  
  10.   TMyArrayOfRecord = array of TMyRecord;
  11.  
  12.   function MyFunctionInt(): TMyArrayOfInteger;
  13.  
  14.   function MyFunctionRec(): TMyArrayOfRecord;
  15.  
To err is human, but to really mess things up, you need a computer.

RayoGlauco

  • Full Member
  • ***
  • Posts: 227
  • Beers: 1567
Re: Array return
« Reply #2 on: August 01, 2025, 01:09:29 pm »
Some examples for the functions:

Code: Pascal  [Select][+][-]
  1. function MyFunctionInt(): TMyArrayOfInteger;
  2. begin
  3.   SetLength(Result,4);
  4.   Result[0]:= 3;
  5.   Result[1]:= 721;
  6.   Result[2]:= 23;
  7.   Result[3]:= 13;
  8. end;
  9.  
  10. function MyFunctionRec(): TMyArrayOfRecord;
  11. begin
  12.   SetLength(Result,4);
  13.   Result[0].number:= 3;
  14.   Result[0].text:= 'foo';
  15.   Result[1].number:= 5;
  16.   Result[1].text:= 'bar';
  17.   Result[2].number:= 7;
  18.   Result[2].text:= 'hello';
  19.   Result[3].number:= 9;
  20.   Result[3].text:= 'world';
  21. end;
  22.  
To err is human, but to really mess things up, you need a computer.

Petrus Vorster

  • Full Member
  • ***
  • Posts: 241
Re: Array return
« Reply #3 on: August 01, 2025, 01:21:27 pm »
Great.

I was also considering making it output a Tsringlist and then just parse it.
Thanks you for the great examples.

-Peter

cdbc

  • Hero Member
  • *****
  • Posts: 2770
    • http://www.cdbc.dk
Re: Array return
« Reply #4 on: August 01, 2025, 01:50:54 pm »
Hi Peter
In case you want to return a stringlist, you should have a look at THIS.
With 'IStringList' you don't have to worry about freeing the returned list... Ex:
Code: Pascal  [Select][+][-]
  1. uses istrlist;
  2. ...
  3. function FindFirstnames(const aMask: string;aQuery: TSqlQuery): IStringlist;
  4. begin
  5.   Result:= CreStrings; { factory from 'istrlist' }
  6.   aQuery.First;
  7.   while not aQuery.EOF do begin
  8.     if aMask = aQuery.FieldbyName('firstname').AsString then
  9.       Result.Append(aQuery.FieldbyName('firstname').AsString);
  10.     aQuery.Next;
  11.   end;
  12. end;
  13.  
Since the result from the function is a managed COM-Interface, you don't have to worry about freeing it, the compiler does that.
HTH
Regards Benny
« Last Edit: August 01, 2025, 01:56:31 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

RayoGlauco

  • Full Member
  • ***
  • Posts: 227
  • Beers: 1567
Re: Array return
« Reply #5 on: August 01, 2025, 02:26:29 pm »
Since the topic has come up, I want to clarify that dynamic arrays do not require memory to be freed either:

https://wiki.freepascal.org/Dynamic_array#handling

Quote
Nonetheless, dynamic arrays are finalized automatically. It is not necessary to manually setLength(…, 0) on all your references when the program comes to end, or when leaving a scope in general.
To err is human, but to really mess things up, you need a computer.

Petrus Vorster

  • Full Member
  • ***
  • Posts: 241
Re: Array return
« Reply #6 on: August 01, 2025, 03:05:53 pm »
Thank you all. I have learned a great deal.

While we are here. Something that i did in Basic and I checked just now that I can indeed do that here as well is to create a Global TYPE record and simply change the elements of that Type anywhere in the project.
Like the current customer record. Then one can access the info no matter where you are in the program.

It is perhaps about 15 strings big.
Is that ok, or is it one of my bad ideas?
Or e.g. a bunch of settings in the INI file that i may need in various places in the program.

Any opinions?

-Peter

 

TinyPortal © 2005-2018