Recent

Author Topic: Pass object/component to procedure  (Read 767 times)

Researching

  • Full Member
  • ***
  • Posts: 121
Pass object/component to procedure
« on: May 17, 2023, 07:44:33 pm »
Need to
pass object to procedure or method and
get it processed by certain operations.


Module_1:
uses Module_2
Procedure SendRecordsToTable(
                          ListOfRecords:array of Records;
                          Table:TStringGrid );
begin
   for i=0 to sizeOf (ListOfRecords)-1 do
       for j=0 to sizeOf(ListOfRecords[j]-1) do begin
                vRow:= i+1;
                vCol := j+1;
                StringToStringGrid
                        (StringGrid, vCol,vRow, ListOfRecords[j]-1 )
Module_2:
procedure StringToStringGrid (StringGrid, vCol, vRow, string)
// this will write the string to StrGrid cells.

Actually need to put records from array or list to StringGrid
But different stringGrid in different Cases.
Actually StringGrid will be located on different Pages of Page Control.
TForm1.PageControl1 ...

Array of Records -> procedure -> StringGrid.

And StringGrid is
StrGr1
StrGr2
StrGr3
etc.
« Last Edit: May 17, 2023, 08:50:45 pm by Researching »

cdbc

  • Hero Member
  • *****
  • Posts: 1083
    • http://www.cdbc.dk
Re: Pass object/component to procedure
« Reply #1 on: May 17, 2023, 08:52:36 pm »
Hi
Something like this:
Code: Pascal  [Select][+][-]
  1. function bcGridSetRowText(aGrid: TStringGrid;aRow: ptrint;RowCells: array of string): ptrint;
  2. var
  3.   I,Cnt: ptrint;
  4.   function min(a,b: ptrint): ptrint; begin if a <= b then Result:= a else Result:= b; end;
  5. begin
  6.   Cnt:= min(aGrid.ColCount,Length(RowCells));
  7.   if Cnt = 0 then exit(-1);
  8.   if aRow = -1 then begin { add a row }
  9.     aRow:= aGrid.RowCount;
  10.     aGrid.RowCount:= aGrid.RowCount + 1;
  11.   end;
  12.   if aRow < aGrid.RowCount then for I:= 0 to Cnt -1 do
  13.     Grid.Cells[I,aRow]:= RowCells[I];
  14.   Result:= aRow;
  15. end;
  16.  
Hth
Regards Benny
« Last Edit: May 17, 2023, 08:56:59 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Researching

  • Full Member
  • ***
  • Posts: 121
Re: Pass object/component to procedure
« Reply #2 on: May 18, 2023, 06:16:16 am »
Not yet clear how it works...
Input data:

object/component: Form1.StringGrid1
Record to fill in
entity = Record
    PKey:integer; //will require IntToStr(entity.PKey)
    Name : string;
    address:string
   end;

Result:

new row in StringGrid.

And the main question is still:

The syntax of how to pass and receive parameters:
from the calling procedure_A (iterating the array of records)
in the call of procedure_B
and receive in the called procedure_B (filling data in StringGrid)

for example:

var
TRecordMy = Record
    PKey:integer; //will require IntToStr(entity.PKey)
    Name : string;
    address:string
   end;
.............................
proc_A ();
begin
proc_B(form1.StringGrid1, myArray[n])
end;


Proc_B(aGrid: TStringGrid; aRecord: TRecordMy)

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: Pass object/component to procedure
« Reply #3 on: May 18, 2023, 06:50:58 am »
To keep it in the spirit of cdbc's answer:
Code: Pascal  [Select][+][-]
  1. type TMyArray = array of TRecordMy;
  2.  
  3. procedure proc(SG: TStringGrid; Items: TMyArray);
  4. var
  5.   Item     : TRecordMy;
  6.   rowindex : integer;
  7.   RowCells : Array of string;
  8. begin
  9.   Rowindex := 1;
  10.   for item in items do
  11.   begin
  12.     RowCells := [Item.PKey.ToString, Item.Name, Item.address];
  13.     bcGridSetRowText(SG, Rowindex, RowCells);
  14.     inc(rowindex);
  15.   end;
  16. end;
  17.  
  18.  

 

TinyPortal © 2005-2018