Recent

Author Topic: [SOLVED] Converting an old Delphi procedure to Lazarus  (Read 823 times)

1HuntnMan

  • Full Member
  • ***
  • Posts: 189
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
[SOLVED] Converting an old Delphi procedure to Lazarus
« on: February 24, 2024, 09:15:43 pm »
I'm finishing up my first Laz project and including a Message of Today when the user closes out of the app.  This is from an old Delphi app I wrote back in the late 1980's.  Some of you experts might see the issue right away.  Here's a snippet, I commented what Laz doesn't like:

Code: Pascal  [Select][+][-]
  1. procedure TFrmCntksMain.FormClose(Sender: TObject;var CloseAction: TCloseAction);
  2. var
  3.   intQuoteCount:integer;
  4. begin
  5.   Action:= caFree; //-> Laz doesn't like this...
  6.   intQuoteCount:= DbfQuotes.FieldByName('QUOTECOUNT').AsInteger;
  7.   Randomize; //-> Laz doesn't like this...
  8.   Case Random(intQuoteCount) of
  9.     0: MessageDlg('My style is not to rely on talent and ability, but to'#13+
  10.                   'bludgeon the competition, to work harder than the other'#13+
  11.                   'guy. No stone left unturned. Attention to detail is how'#13+
  12.                   'I get there.'#13+
  13.                   '                                - Skipper Dennis Conner',
  14.         mtInformation,[mbOK],0);
  15.     1: MessageDlg('Great Spirits Have Always Encountered'#13+
  16.                   ' Violent Opposition From Mediocre Minds!'#13+
  17.                   '                        - Albert Einstein',
  18. and so on for 0 thru 57 quotes, i.e. intQuoteCount = 58
  19.  

Tks in advance,
1HuntnMan
« Last Edit: March 07, 2024, 11:38:24 pm by 1HuntnMan »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Converting an old Delphi procedure to Lazarus
« Reply #1 on: February 24, 2024, 09:20:34 pm »
I'm finishing up my first Laz project and including a Message of Today when the user closes out of the app.  This is from an old Delphi app I wrote back in the late 1980's. 

Interesting since Delphi 1 is from 1995.

Code: Pascal  [Select][+][-]
  1.    Action:= caFree; //-> Laz doesn't like this...
  2.  

Should probably be closeaction. Possible hidden bug in old code that sets some random unrelated property.


Code: Pascal  [Select][+][-]
  1.   Randomize; //-> Laz doesn't like this...
  2.  

Randomize is in the system unit on most but the most embedded targets. So something else is going on there.

Note though that calling randomize everytime before random() is not proper usage, in FPC nor Delphi

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: Converting an old Delphi procedure to Lazarus
« Reply #2 on: February 24, 2024, 09:22:40 pm »
Next time... Please state the exact error message.
I'm sure Lazarus doesn't say "I don't like this"  :P

Action:= caFree;
Are you sure that's not CloseAction := caFree ?
Maybe the parameter in Delphi for FormClose is different.
In Lazarus it is CloseAction (as you can see in the parameters).

Randomize
This shouldn't be a problem.
https://www.freepascal.org/docs-html/rtl/system/randomize.html

What's the exact error message?

Edit: Marco is just a bit faster  ;)

1HuntnMan

  • Full Member
  • ***
  • Posts: 189
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Converting an old Delphi procedure to Lazarus
« Reply #3 on: February 24, 2024, 10:15:26 pm »
Sorry about that RVK, here's the line with the Laz error underneath from previous code:

Action:= caFree;
Error: Incompatible type for arg no. 1: Got "TCloseAction", expected "TBasicAction"
Hint: Found declaration: SetAction(TBasicAction);

Also, copied this from an app I wrote long time ago in Delphi. Actually, RFK, your reply made me re-look at the my old Delphi code.  The old Delphi Procedure was this:

procedure TExecuTracksMainFm.FormClose(Sender: TObject; var Action: TCloseAction);

My Laz Procedure:
procedure TFrmCntksMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);

So, when you replied, "Shouldn't it be CloseAction := caFree?"

You are exactly right, I wasn't paying attention to the way Delphi constructed the FormClose procedure statement vs. Laz's FormClose procedure statement, Action: TCloseAction vs. CloseAction: TCloseAction!!!!

Thanks also for the correction in time, too long ago when I was programming in Delphi 5, 6, then 7 to remember the years.

Correction: I was programming with Turbo Pascal and Clipper back in the 1980's and Delphi in the 1990's.

1HuntnMan

  • Full Member
  • ***
  • Posts: 189
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Converting an old Delphi procedure to Lazarus
« Reply #4 on: February 24, 2024, 10:17:41 pm »
Here's the change with no error:
  CloseAction:= caFree;

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Converting an old Delphi procedure to Lazarus
« Reply #5 on: February 24, 2024, 10:23:49 pm »
Code: Pascal  [Select][+][-]
  1. procedure TFrmCntksMain.FormClose(Sender: TObject;var CloseAction: TCloseAction);
  2. var
  3.   intQuoteCount:integer;
  4. begin
  5. ...
  6.     1: MessageDlg('Great Spirits Have Always Encountered'#13+
  7.                   ' Violent Opposition From Mediocre Minds!'#13+
  8.                   '                        - Albert Einstein',
  9. and so on for 0 thru 57 quotes, i.e. intQuoteCount = 58
  10.  
That's just terrible (58 MessageDlg's in 1 procedure).
Why not have an array (e.g. named MyMessages) holding all 58 strings and then simply MessageDlg(MyMessages[Random(intQuoteCount)], ...)?

Bart

1HuntnMan

  • Full Member
  • ***
  • Posts: 189
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Converting an old Delphi procedure to Lazarus
« Reply #6 on: February 24, 2024, 11:47:14 pm »
Bart,
   Good idea, just the way I did it way back when. Thanks for the suggestion.
   It's just terrible! LoL
Take care, 1HuntnMan

VisualLab

  • Sr. Member
  • ****
  • Posts: 336
Re: Converting an old Delphi procedure to Lazarus
« Reply #7 on: February 25, 2024, 01:02:02 pm »
Action:= caFree;
Are you sure that's not CloseAction := caFree ?
Maybe the parameter in Delphi for FormClose is different.
In Lazarus it is CloseAction (as you can see in the parameters).

Yes, there is a difference in the argument name for the method generated for the OnClose event. I just checked (in Windows 10).

For Delphi (version 12):

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

For Lazarus (3.0):

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);

All you need is a minor correction to the name of the argument used.

VisualLab

  • Sr. Member
  • ****
  • Posts: 336
Re: Converting an old Delphi procedure to Lazarus
« Reply #8 on: February 25, 2024, 01:06:16 pm »
Code: Pascal  [Select][+][-]
  1. procedure TFrmCntksMain.FormClose(Sender: TObject;var CloseAction: TCloseAction);
  2. var
  3.   intQuoteCount:integer;
  4. begin
  5. ...
  6.     1: MessageDlg('Great Spirits Have Always Encountered'#13+
  7.                   ' Violent Opposition From Mediocre Minds!'#13+
  8.                   '                        - Albert Einstein',
  9. and so on for 0 thru 57 quotes, i.e. intQuoteCount = 58
  10.  
That's just terrible (58 MessageDlg's in 1 procedure).
Why not have an array (e.g. named MyMessages) holding all 58 strings and then simply MessageDlg(MyMessages[Random(intQuoteCount)], ...)?

Bart

With this amount of text, it might even be better to put it in some text file. For example, one line per message. And then this can be easily loaded into a list of type TStringList. And it will be easy to index individual texts.

 

TinyPortal © 2005-2018