Recent

Author Topic: Static and dynamic array as Parameters for Function  (Read 524 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Static and dynamic array as Parameters for Function
« on: May 03, 2021, 04:38:13 pm »
Hello Guys :)

So lately i tried to write a Sorting algorithm for my ListBoxes, bc i need them to be sorted by Relevance.

So i have like 26 Classes where i call this Sort algorithm but i always have a different Length of my Array. How do i pass a Static array to a Fucntion wich recieves Dynamic Arrays, or do i have to bring Length also as Parameter ?

what i have right now :

Code: Pascal  [Select][+][-]
  1.   //Definitions
  2.   SelectedZubehoer = record
  3.      Name : String;
  4.      Ausgewaelt : Boolean;
  5.      ID : Integer;
  6.   end;
  7.  
  8.   TSelZub = Array of SelectedZubehoer;
  9.  
  10.   //Call
  11.   myGlobalClass.SortZub(SelZub, ZubListe1, ZubListe2);
  12.  
  13.   //Function
  14.   Procedure SortZub(SZ : TSelZub; var zubliste1, zubliste2 :
  15.   TListBox);
  16.  
  17.   begin
  18.  
  19.    ZubListe1.Clear;
  20.    ZubListe2.Clear;
  21.  
  22.    //Sorting
  23.  
  24. end;
  25.  

How should i do it ?
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Static and dynamic array as Parameters for Function
« Reply #1 on: May 03, 2021, 05:31:03 pm »
Use open array parameters. You can then pass any array to it, whether static or dynamic.

Code: Pascal  [Select][+][-]
  1.   //Function
  2.   Procedure SortZub(SZ : Array of SelectedZubehoer;
  3.       var zubliste1, zubliste2 : TListBox);
  4.  
  5.   begin
  6.  
  7.    ZubListe1.Clear;
  8.    ZubListe2.Clear;
  9.  
  10.    //Sorting
  11.   end;

Note that it's not the same as using:
Code: Pascal  [Select][+][-]
  1. Procedure SortZub(SZ : TSelZub; ...)
since in this case the rules for type compatibility become more strict.
« Last Edit: May 03, 2021, 05:37:15 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018