Recent

Author Topic: [RESOLVED]Event handler seems not to allow local variables  (Read 683 times)

tfurnivall

  • New Member
  • *
  • Posts: 49
[RESOLVED]Event handler seems not to allow local variables
« on: June 02, 2025, 10:35:29 pm »
I have an OnClick handler defined as follows:
Code: Pascal  [Select][+][-]
  1. procedure TfrmFOpenTool.cboFDomainClick(Sender: TObject);
  2. var
  3.     ix    : integer;
  4.     ic    : integer;
  5. begin
  6.     ic :=TfrmFOpenTool.cboFDomain.items.Count;
  7.     ix :=TfrmFOpenTool.cboFDomain.itemindex;
  8.  
  9.  
  10. end;
  11.  

I'm trying to grab the values of the ItemsCount and the ItemIndex.  I have seen other handlers which declare local variables, and everything seems to work. However, my own attempt fails - miserably!

Code: Pascal  [Select][+][-]
  1. Only class methods, class properties and class variables can be referred with class references
  2. Only class methods, class properties and class variables can be accessed in class methods
  3.  
  4.  
  5.  

Seems pretty clear - but the highlighted portion of the offending line is around the control name.

The full class definition is:
Code: Pascal  [Select][+][-]
  1.     TfrmFOpenTool = class(TForm)
  2.         cboFName: TComboBox;
  3.         cboFName1: TComboBox;
  4.         cboFName2: TComboBox;
  5.         cmdExit: TButton;
  6.         cmdBrowse: TButton;
  7.         cboFDomain: TComboBox;
  8.         lblDeviceValue: TLabel;
  9.         lblName: TLabel;
  10.         lblStructure: TLabel;
  11.         lblOrganization: TLabel;
  12.         lblNameValue: TLabel;
  13.         lblFOptions1: TLabel;
  14.         lblStructureValue: TLabel;
  15.         lblOrganizationValue: TLabel;
  16.         lblPriority: TLabel;
  17.         lblPriorityValue: TLabel;
  18.         lblIndexCount: TLabel;
  19.         lblIndexCountValue: TLabel;
  20.         lblDomain: TLabel;
  21.         lblRecSize: TLabel;
  22.         lblDomainValue: TLabel;
  23.         lblAccess: TLabel;
  24.         lblAccessValue: TLabel;
  25.         lblDevice: TLabel;
  26.         lblRecSizeValue: TLabel;
  27.         lblDOptions: TLabel;
  28.         lblAOptions: TLabel;
  29.         lblROptions: TLabel;
  30.         lblFOptions: TLabel;
  31.         txtFileName: TEdit;
  32.         txtFileEquation: TEdit;
  33.         lblFileName: TLabel;
  34.         lblFEQ: TLabel;
  35.         PageControl1: TPageControl;
  36.         pgFOptions: TTabSheet;
  37.         pgROptions: TTabSheet;
  38.         pgAOptions: TTabSheet;
  39.         pgDOptions: TTabSheet;
  40.         pgSpoolFile: TTabSheet;
  41.         pgIndexing: TTabSheet;
  42.         procedure cboFDomainChange(Sender: TObject);
  43.         procedure cboFDomainClick(Sender: TObject);
  44.         procedure cmdExitClick(Sender: TObject);
  45.         procedure FormCreate(Sender: TObject);
  46.         procedure pgFOptionsContextPopup(Sender: TObject; MousePos: TPoint;
  47.             var Handled: Boolean);
  48.         procedure pgROptionsContextPopup(Sender: TObject; MousePos: TPoint;
  49.             var Handled: Boolean);  

Can anyone explain why I'm getting these errors.
« Last Edit: June 02, 2025, 11:56:36 pm by tfurnivall »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11443
  • Debugger - SynEdit - and more
    • wiki
Re: Event handler seems not to allow local variables
« Reply #1 on: June 02, 2025, 10:45:27 pm »
Don't prefix the field name with the classname
Quote
Code: Pascal  [Select][+][-]
  1.     ic :=TfrmFOpenTool.cboFDomain.items.Count;
  2.     ix :=TfrmFOpenTool.cboFDomain.itemindex;

should be
Code: Pascal  [Select][+][-]
  1.     ic :=cboFDomain.items.Count;
  2.     ix :=cboFDomain.itemindex;


If you have a global var
Code: Pascal  [Select][+][-]
  1. var frmFOpenTool1: TfrmFOpenTool

And if that is initialized (e.g. by the IDE generated code) then you can do

Code: Pascal  [Select][+][-]
  1.     ic :=frmFOpenTool1.cboFDomain.items.Count;
  2.     ix :=frmFOpenTool1.cboFDomain.itemindex;

Note: the variable, not the class.


But you do not need that in any method of the class.

You would need that in procedures that don't have the "TfrmFOpenTool." prefix in their name.

tfurnivall

  • New Member
  • *
  • Posts: 49
Re: Event handler seems not to allow local variables
« Reply #2 on: June 02, 2025, 11:50:23 pm »
Thanks, Martin_fr,

Yes - that made sense (and the compiler approves)! The error was actually pointing past the cboFDomain bit, so I assumed it was the next symbol that was causing the error. Is there any Me construct in Lazarus (analagous to the Me in VBA)?

And is there any good reference for LCL controls? It's almost as if the developers can't believe that anybody wouldn't know that stuff! I've grumbled about Microsoft documentation, and Lazarus is - in general - better and more detailed (even if occasionally not fully comprehensible). However the controls are not a good example :(

Thanks for the guidance - I'm certain that there will be more requests!

Tony

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11443
  • Debugger - SynEdit - and more
    • wiki
Re: Event handler seems not to allow local variables
« Reply #3 on: June 03, 2025, 12:55:43 am »
Yes - that made sense (and the compiler approves)! The error was actually pointing past the cboFDomain bit, so I assumed it was the next symbol that was causing the error. Is there any Me construct in Lazarus (analagous to the Me in VBA)?

Not familiar with VBA, but I guess that is "self"

   self.SomeField


Docs:
- https://lazarus-ccr.sourceforge.io/docs/lcl/index.html
- Our wiki (link in the menu on the left)
- various tutorials on Pascal/Object Pascal, like https://www.delphibasics.co.uk/index.html

n7800

  • Sr. Member
  • ****
  • Posts: 394
Re: [RESOLVED]Event handler seems not to allow local variables
« Reply #4 on: June 03, 2025, 12:58:27 am »
Is there any Me construct in Lazarus (analagous to the Me in VBA)?

There is Self, see docs and docs.

And is there any good reference for LCL controls? It's almost as if the developers can't believe that anybody wouldn't know that stuff! I've grumbled about Microsoft documentation, and Lazarus is - in general - better and more detailed (even if occasionally not fully comprehensible). However the controls are not a good example :(

The easiest way is to select a property in the Object Inspector (property grid) and press [F1]. The link will open in a browser (site).

There is also an additional package for viewing documentation offline (see wiki).

A little late with the answer ))

tfurnivall

  • New Member
  • *
  • Posts: 49
Re: [RESOLVED]Event handler seems not to allow local variables
« Reply #5 on: June 03, 2025, 01:21:25 am »
Ah, Self! (That would be Me ;-)

Thanks, Martin-fr, and n7800. These forums are worth their weight in platinum!

Tony

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Re: [RESOLVED]Event handler seems not to allow local variables
« Reply #6 on: June 03, 2025, 08:20:41 am »
Ah, Self! (That would be Me ;-)

Thanks, Martin-fr, and n7800. These forums are worth their weight in platinum!

Tony

Addition:
There is the "Sender"-Argument in the Event-Handler, which represents the Object causing the Event to be fired, in your case the Combobox itself.
You could even go like this:
Code: Pascal  [Select][+][-]
  1. procedure TfrmFOpenTool.cboFDomainClick(Sender: TObject);
  2. var
  3.     ix    : integer;
  4.     ic    : integer;
  5. begin
  6.     ic :=Sender.items.Count;
  7.     ix :=Sender.itemindex;
  8. end;
Comments of the others not withstanding (No Sanity-Check on ItemIndex etc.)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 17379
  • Ceterum censeo Trump esse delendam
Re: [RESOLVED]Event handler seems not to allow local variables
« Reply #7 on: June 03, 2025, 09:36:48 am »
The latter code won't work without a hard or soft cast, because TObject does not have those properties.
Code: Pascal  [Select][+][-]
  1. procedure TfrmFOpenTool.cboFDomainClick(Sender: TObject);
  2. var
  3.     ix, ic : integer;
  4. begin
  5.     ic :=(Sender as TComboBox).items.Count;
  6.     ix :=(Sender as TComboBox).itemindex;
  7. end;
In this case you have my permission to use a with statement.
alternative:
Code: Pascal  [Select][+][-]
  1. procedure TfrmFOpenTool.cboFDomainClick(Sender: TObject);
  2. var
  3.     ix    : integer;
  4.     ic    : integer;
  5. begin
  6.     if Sender is TComboBox then with TCombobox(Sender) do
  7.     begin
  8.        ic :=items.Count;
  9.        ix :=itemindex;
  10.     end;
  11. end;
« Last Edit: June 03, 2025, 09:50:01 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Zvoni

  • Hero Member
  • *****
  • Posts: 3001
Re: [RESOLVED]Event handler seems not to allow local variables
« Reply #8 on: June 03, 2025, 11:22:07 am »
The latter code won't work without a hard or soft cast, because TObject does not have those properties.
Ah yes......
Correct. Always forget that, and then the compiler reminds me of it again.. :D
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

n7800

  • Sr. Member
  • ****
  • Posts: 394
Re: [RESOLVED]Event handler seems not to allow local variables
« Reply #9 on: June 03, 2025, 02:19:07 pm »
You can use "absolute" if you are sure of the "Sender" type:

Code: Pascal  [Select][+][-]
  1. procedure TfrmFOpenTool.cboFDomainClick(Sender: TObject);
  2. var
  3.     lCombo: TComboBox absolute Sender;
  4.     ix, ic : integer;
  5. begin
  6.     ic :=lCombo.Items.Count;
  7.     ix :=lCombo.ItemIndex;
  8. end;
  9.  

Thaddy

  • Hero Member
  • *****
  • Posts: 17379
  • Ceterum censeo Trump esse delendam
Re: [RESOLVED]Event handler seems not to allow local variables
« Reply #10 on: June 03, 2025, 02:44:15 pm »
That works too, although absolute has absolutely disadvantages.
But not in this case.
Actually, here it is quite neat!
One remark: there is no verification that sender is really a combobox......
......and will get you into trouble if you connect the event to other components too.... Just a warning.
« Last Edit: June 03, 2025, 02:48:09 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018