Recent

Author Topic: For loop only runs for last element  (Read 2246 times)

randohinn

  • New Member
  • *
  • Posts: 13
For loop only runs for last element
« on: August 15, 2020, 10:47:36 pm »
Hmm.. A bit of a pickle here... This for loop only seems to show the message (and do anything else) for the last component that's casted.. What could be causing this?

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.FormResize(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   tmp: WorkTimeRow.TfrmWorkTimeRow;
  5. begin
  6.   for i := 0 to (scrlRows.ComponentCount-1) do
  7.         tmp := scrlRows.Components[i] as WorkTimeRow.TfrmWorkTimeRow;
  8.         ShowMessage(DateToStr(tmp.edtDate.Date));
  9.         tmp.Width:=scrlRows.ClientWidth;
  10.         tmp.FrameResize(Sender);
  11.  
  12. end;
  13.  
  14.  

scrlRows is a TScrollBox and WorkTimeRow.TfrmWorkTimeRow is a TFrame, for which I created a procedure called FrameResize to handle positioning some stuff around inside. I need to run this on all the frames inside the scrollbox, but as is, it only runs for the last one :(

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: For loop only runs for last element
« Reply #1 on: August 15, 2020, 11:05:22 pm »
Hmm.. A bit of a pickle here... This for loop only seems to show the message (and do anything else) for the last component that's casted.. What could be causing this?
Because that is all the loop is doing. The showmessage is invoked after the loop finished.


randohinn

  • New Member
  • *
  • Posts: 13
Re: For loop only runs for last element
« Reply #2 on: August 15, 2020, 11:10:25 pm »
Okay.. Removing the debug showmessage.. And everything else still works only for the last component :/

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: For loop only runs for last element
« Reply #3 on: August 15, 2020, 11:13:59 pm »
OK, sorry. I thought it was so obvious that it would be enough to understand what is wrong with just providing a hint. :-[

If you want to run a multiple amount of code-lines inside a loop you need to place them between a begin...end block.

edit: add corrected code
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.FormResize(Sender: TObject);
  2. var
  3.   i: Integer;
  4.   tmp: WorkTimeRow.TfrmWorkTimeRow;
  5. begin
  6.   for i := 0 to (scrlRows.ComponentCount-1) do
  7.   begin   // <- add this
  8.         tmp := scrlRows.Components[i] as WorkTimeRow.TfrmWorkTimeRow;
  9.         ShowMessage(DateToStr(tmp.edtDate.Date));
  10.         tmp.Width:=scrlRows.ClientWidth;
  11.         tmp.FrameResize(Sender);
  12.   end; // <- add this
  13. end;
  14.  
Now all lines between the begin..end are executed for every iteration of the for-loop
« Last Edit: August 15, 2020, 11:18:04 pm by TRon »

wp

  • Hero Member
  • *****
  • Posts: 7642
Re: For loop only runs for last element
« Reply #4 on: August 15, 2020, 11:28:02 pm »
Pascal is not Python.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

ASBzone

  • Sr. Member
  • ****
  • Posts: 476
  • Automation leads to relaxation...
    • Free Console Utilities for Windows from BrainWaveCC
Re: For loop only runs for last element
« Reply #5 on: August 16, 2020, 04:03:04 am »
Pascal is not Python.

One of the drawbacks of using multiple systems or development languages is that until you become really proficient in all of the ones you are using, or you use them regularly enough, you sometimes think in one while actually writing in another.  :)
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.11 r64032 / FPC v3.2.1-r47152 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 2009 (Build 19042.572)
Other Systems: Windows 10 Pro x64, Version 2004 or greater

Blade

  • New Member
  • *
  • Posts: 46
Re: For loop only runs for last element
« Reply #6 on: August 16, 2020, 07:52:49 am »
Pascal is not Python.

Truth.

Quote
If you want to run a multiple amount of code-lines inside a loop you need to place them between a begin...end block.

And it works that way with C/C++ too, with their {...}.  So definitely looks like the OP has a Python-like habit.

Warfley

  • Sr. Member
  • ****
  • Posts: 316
Re: For loop only runs for last element
« Reply #7 on: August 16, 2020, 03:39:00 pm »
Also it should be noted that depending on the typing that can go horribly wrong.

If your size value is an unsigned type (like SizeUInt, which for example is used in TVector), this can result in an maxint loop:
Code: Pascal  [Select][+][-]
  1. var i, sz: SizeUInt;
  2. begin
  3.   sz := 0;
  4.   for i:=0 to sz-1 do
  5.     WriteLn(i);
  6. end.

and therefore requires special 0 handling:
Code: Pascal  [Select][+][-]
  1. var i, sz: SizeUInt;
  2. begin
  3.   sz := 0;
  4.   if sz > 0 then for i:=0 to sz-1 do
  5.     WriteLn(i);
  6. end.

 

TinyPortal © 2005-2018