Recent

Author Topic: [solved] looping direction controlled by flag  (Read 1460 times)

Josh

  • Hero Member
  • *****
  • Posts: 1374
Re: looping direction controlled by flag
« Reply #15 on: April 19, 2025, 09:42:47 am »
indeed,
adds more code but function can be tided up,
even the directiion can be set in the create

Code: Pascal  [Select][+][-]
  1. TControlIterator = class
  2.   private
  3.     F_Controls: TArray<TControl>;
  4.     F_Index: Integer;
  5.     F_Step: Integer;
  6.     F_Low, F_High: Integer;
  7. ........
  8. ........
  9. constructor TControlIterator.Create(const Controls: TArray<TControl>; IsFirst: Boolean);
  10. begin
  11.   F_Controls := Controls;
  12.   F_Low := Low(Controls);
  13.   F_High := High(Controls);
  14.  
  15.   if IsFirst then
  16.   begin
  17.     F_Index := F_Low;  // Start from the beginning
  18.     F_Step := 1;      // Move forward
  19.   end
  20.   else
  21.   begin
  22.     F_Index := F_High; // Start from the end
  23.     F_Step := -1;     // Move backward
  24.   end;
  25. end;                  
  26.  
  27. .......
  28.  
  29.  

and function
Code: Pascal  [Select][+][-]
  1. function GetTheControl(const IsFirst: Boolean): TControl;
  2. var
  3.   Flight: TControlIterator;
  4.   Cntrl: TControl;
  5. begin
  6.   Flight := TControlIterator.Create(AR_CONTROLS, IsFirst);
  7.   try
  8.     while Flight.HasNext do
  9.     begin
  10.       Cntrl := Flight.Next;
  11.       if Provides_SQL(Cntrl) then  Exit(Cntrl);
  12.     end;
  13.   finally
  14.     Flight.Free;
  15.   end;
  16.   Error_Halt('No matching control found.');
  17. end;
  18. end;  
« Last Edit: April 19, 2025, 01:28:41 pm by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1422
Re: looping direction controlled by flag
« Reply #16 on: April 19, 2025, 07:07:08 pm »
Hi
Another possibility would be to utilize 'case', like this:
Code: Pascal  [Select][+][-]
  1. var x: integer;
  2. begin
  3.   case IsFirst of
  4.     false: begin
  5.              for x:= ControlCount-1 downto 0 do
  6.                if Controls[x].Name = 'Memo1' then exit(Controls[x]);
  7.            end;
  8.     true: begin
  9.             for x:= 0 to ControlCount-1 do
  10.               if Controls[x].Name = 'Edit1' then exit(Controls[x]);
  11.           end
  12.   end;
  13.   Result:= nil;
  14.   fPresenter.Provider.NotifySubscribers(prStatus,nil,Str2Pch('(!) Attention: Control was not found! '));
  15. end;
  16.  
I hope you get the gist of the algorithm, 'cause I really don't wanna mess with your caps-locked-code...
Regards Benny
Or this
Code: Pascal  [Select][+][-]
  1.  FUNCTION TCASCADING_FLOWPANEL.GET_THE_CONTROL(CONST IS_FIRST:BOOLEAN):TCONTROL;
  2.          VAR X:INTEGER;
  3. BEGIN
  4. CASE IS_FIRST OF
  5.         TRUE: FOR X:= LOW(AR_CONTROLS) TO HIGH(AR_CONTROLS) DO
  6.                          IF PROVIDES_SQL(AR_CONTROLS[X])THEN EXIT(AR_CONTROLS[X]);
  7.          FALSE: FOR X:= HIGH(AR_CONTROLS) DOWNTO LOW(AR_CONTROLS) DO
  8.                            IF PROVIDES_SQL(AR_CONTROLS[X])THEN EXIT(AR_CONTROLS[X]);
  9.           END;
  10. ERROR_HALT('FUNCTION TCASCADING_FLOWPANEL.GET_THE_CONTROL');
  11. END;  
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

cdbc

  • Hero Member
  • *****
  • Posts: 2150
    • http://www.cdbc.dk
Re: looping direction controlled by flag
« Reply #17 on: April 19, 2025, 07:14:48 pm »
Hi Joanna
Yup, I think you've got it =^
I'll just put on my shades  8), so I don't get an 'Eye-Sore'...  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

440bx

  • Hero Member
  • *****
  • Posts: 5316
Re: looping direction controlled by flag
« Reply #18 on: April 19, 2025, 08:08:37 pm »
For some reason, Pascal code looks horrible in all uppercase.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1422
Re: looping direction controlled by flag
« Reply #19 on: April 20, 2025, 01:15:47 pm »
cdbc i liked the idea of the case because even though my
two if statements could never both be true  its still sort of sloppy to call both of them. The case gives less ambiguity in what will happen if thats the correct term...
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

cdbc

  • Hero Member
  • *****
  • Posts: 2150
    • http://www.cdbc.dk
Re: [solved] looping direction controlled by flag
« Reply #20 on: April 20, 2025, 01:20:04 pm »
Hi Joanna
Yup, I think 'case' makes for clear code.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018