Recent

Author Topic: Taking 'Common' parts out of a Procedure  (Read 349 times)

J-G

  • Hero Member
  • *****
  • Posts: 992
Taking 'Common' parts out of a Procedure
« on: February 15, 2026, 05:08:30 pm »
It's a while since I've needed to do any Pascal programming and it seems that I've forgotten some basic facts :)

In an old project that I'm re-visiting I have one procedure which clears all the input data in one fell swoop, but I'd now like to select which input fields to clear.

The fact that clearing any one field actually invalidates 4 'common' fields (results) means that it make sense to always clear those fields, so to this end I created a new procedure 'Clear_common' to be called whenever any input field is cleared.

Here is the original procedure :
Code: Pascal  [Select][+][-]
  1. procedure TTriangleSolution.ClearClick(Sender: TObject);
  2. begin
  3.   Val_A.text      := blank;
  4.   Val_A1.text     := blank;
  5.   Val_B.text      := blank;
  6.   Val_B1.text     := blank;
  7.   Val_C.text      := blank;
  8.   Val_C1.text     := blank;
  9.   Val_alpha.text  := blank;
  10.   Val_alpha1.text := blank;
  11.   Val_beta.text   := blank;
  12.   Val_beta1.text  := blank;
  13.   Val_gamma.text  := blank;
  14.   Val_gamma1.text := blank;
  15.  
  16.   Val_Area.text  := blank;
  17.   Val_Peri.text  := blank;
  18.   Val_Height.Text:= blank;
  19.   Tri_type.text  := tri_name[0];
  20.   MessageBoard.caption :=  'Enter at least one side but three values altogether. Anything above 3 will be ignored';
  21. end;
  22.  

The last five lines are the common aspects so here is the new Proc. (which I've inserted above the original Proc.):
Code: Pascal  [Select][+][-]
  1. procedure Clear_Common;
  2. begin
  3.   Tri_type.text  := tri_name[0];
  4.   Val_Area.text  := blank;
  5.   Val_Peri.text  := blank;
  6.   Val_Height.Text:= blank;
  7.  
  8.   MessageBoard.caption :=  'Enter at least one side but three values altogether. Anything above 3 will be ignored';
  9. end;
  10.  

and I've simply added [Clear_Common] in place of those 5 lines.

At compilation I now get an error - pointing to the first line of the new Proc. -
tri.pas(999,3) Error: Identifier not found "Tri_type".

I initially assumed that I needed to add 'TTriangleSolution.'  before Clear_common but that returned a different error message - pointing to the Proc. Header :
tri.pas(997,29) Error: method identifier expected

I'd appreciate some pointer as to what I've forgotten :)  or more to the point, how I can resolve the matter!



FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

dseligo

  • Hero Member
  • *****
  • Posts: 1672
Re: Taking 'Common' parts out of a Procedure
« Reply #1 on: February 15, 2026, 05:30:35 pm »
I initially assumed that I needed to add 'TTriangleSolution.'  before Clear_common but that returned a different error message - pointing to the Proc. Header :
tri.pas(997,29) Error: method identifier expected

You are correct, but you also need to have that declaration at the top, where your form TTriangleSolution type is declared.
Or you can just type:
Code: Pascal  [Select][+][-]
  1. procedure TTriangleSolution.Clear_Common;

Position cursor in that line and press Shift + Ctrl + C (or right click in that line, Source, Complete Code).

P.S.: Search your code for 'ClearClick' and see how it's declared at the top of your unit and where the actual method is. Just do the same for Clear_Common.
« Last Edit: February 15, 2026, 05:32:30 pm by dseligo »

J-G

  • Hero Member
  • *****
  • Posts: 992
Re: Taking 'Common' parts out of a Procedure
« Reply #2 on: February 15, 2026, 05:50:42 pm »
Bingo!

But of course you knew that @dseligo :)  -  Many thanks

I'm so grateful for this forum as a resource - About 1½ hours fafing about and I get an answer complete with an easy means to correct the error within 25 minutes !

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12195
  • Debugger - SynEdit - and more
    • wiki
Re: Taking 'Common' parts out of a Procedure
« Reply #3 on: February 15, 2026, 05:57:21 pm »
https://wiki.lazarus.freepascal.org/Lazarus_IDE_Tools#Class_Completion

And read the rest of the page too, lots of cool tools.



Also, select any part of your procedure, right click, sub menu "refactoring" > "Extract procedure".
And it will ask you if you want a nested procedure, a method, a plain procedure, ...
https://wiki.lazarus.freepascal.org/Lazarus_IDE_Tools#Extract_Procedure (same page)

J-G

  • Hero Member
  • *****
  • Posts: 992
Re: Taking 'Common' parts out of a Procedure
« Reply #4 on: February 15, 2026, 07:01:17 pm »
Thanks @Martin_Fr,

I have had a quick look at both pages that your links connect to and I'm sure thare are a wealth of tools which may well speed up program creation but for my very simple needs the time taken to research each and remember what is available when - nay IF  - I ever need to use such is probably in-effective. 

I haven't created a new program for at least 2 years (maybe longer) so the chance that I might remember even the [Ctrl][Shift][C] is at best slim  :D

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

 

TinyPortal © 2005-2018