Recent

Author Topic: How to call a procedure, from a procedure  (Read 2554 times)

stephanos

  • New Member
  • *
  • Posts: 12
How to call a procedure, from a procedure
« on: July 07, 2021, 09:40:44 pm »
Dear All

I have written a playlist programme for my mp3 player in Lazarus 2.0.8.  It opens an M3U file on the player and writes the content to a Memo
Code: Pascal  [Select][+][-]
  1. Memo1.Lines.LoadFromFile('PlayListStephen.m3u');
After which it closes the file.  When the Memo window is updated with new content the content is saved to the file using a Save button.  Then I press an Exit button to leave the programme.

The improvement I want to make is to make Exit call Save, save, then exit.  Simple I thought.  From within the Exit onClick event I simply type: “BitBtn5Click();” - see below.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn1Click(Sender: TObject);
  2. begin
  3.   BitBtn5Click();
  4.   Close;
  5. end;  
  6.  

This should call the Save button

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn5Click(Sender: TObject);
  2. begin
  3.        Lots of code to do with saving the Memo1 content
  4. end;
  5.  

But no, this is not the way.  On Compile I get this error message:
“Compile Project, Target: SimpleForms.exe: Exit code 1, Errors: 2, Hints: 1
simpleformsunit.pas(48,64) Hint: Parameter "Value" not used
simpleformsunit.pas(338,17) Error: Wrong number of parameters specified for call to "BitBtn5Click"
simpleformsunit.pas(176,18) Error: Found declaration: BitBtn5Click(TObject);”

According to lots of places, what I have done is correct.

Any help appreciated

ccrause

  • Sr. Member
  • ****
  • Posts: 431
Re: How to call a procedure, from a procedure
« Reply #1 on: July 07, 2021, 09:55:01 pm »
8<
The improvement I want to make is to make Exit call Save, save, then exit.  Simple I thought.  From within the Exit onClick event I simply type: “BitBtn5Click();” - see below.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn1Click(Sender: TObject);
  2. begin
  3.   BitBtn5Click();
  4.   Close;
  5. end;  
  6.  
8<

But no, this is not the way.  On Compile I get this error message:
“Compile Project, Target: SimpleForms.exe: Exit code 1, Errors: 2, Hints: 1
simpleformsunit.pas(48,64) Hint: Parameter "Value" not used
simpleformsunit.pas(338,17) Error: Wrong number of parameters specified for call to "BitBtn5Click"
simpleformsunit.pas(176,18) Error: Found declaration: BitBtn5Click(TObject);”
The compiler is telling you exactly what is missing: the parameter to the method BitBtn5Click.  In code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn1Click(Sender: TObject);
  2. begin
  3.   BitBtn5Click(Sender); // or BitBtn5Click(nil) may also work if you don't use the Sender parameter
  4.   Close;
  5. end;

Quote
According to lots of places, what I have done is correct.
:o

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 985
    • Lebeau Software
Re: How to call a procedure, from a procedure
« Reply #2 on: July 07, 2021, 09:58:49 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn1Click(Sender: TObject);
  2. begin
  3.   BitBtn5Click(Sender); // or BitBtn5Click(nil) may also work if you don't use the Sender parameter
  4.   Close;
  5. end;

Unless BitBtn5Click() is actually using its own Sender parameter, I would pass nil:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn1Click(Sender: TObject);
  2. begin
  3.   BitBtn5Click(nil);
  4.   Close;
  5. end;

The alternative would be to call the button's Click() method instead, and let it call the OnClick handler:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn1Click(Sender: TObject);
  2. begin
  3.   BitBtn5.Click;
  4.   Close;
  5. end;

Though,  a better design would be to separate the save logic to its own method that multiple UI handlers can call when needed, eg:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.BitBtn1Click(Sender: TObject);
  2. begin
  3.   Save;
  4.   Close;
  5. end;  
  6.      
  7. procedure TForm1.BitBtn5Click(Sender: TObject);
  8. begin
  9.   Save;
  10. end;
  11.  
  12. procedure TForm1.Save;
  13. begin
  14.   // Lots of code to do with saving the Memo1 content
  15. end;
« Last Edit: July 07, 2021, 10:01:43 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

stephanos14

  • New member
  • *
  • Posts: 6
Re: How to call a procedure, from a procedure
« Reply #3 on: July 08, 2021, 01:13:23 pm »
Dear All

Thanks, that works.  I will do some research on the last suggestion about method.

Meanwhile, it has highlighted another problem which I will post to the "Using the Lazarus IDE".

 

TinyPortal © 2005-2018