* * *

Author Topic: procedure of object of object?  (Read 1259 times)

Edson

  • Hero Member
  • *****
  • Posts: 806
procedure of object of object?
« on: February 23, 2017, 06:11:10 pm »
I would like yo use a event handler declared nested in a procedure:

Code: Pascal  [Select]
  1. procedure Foo;
  2.  
  3.   procedure Bar;
  4.   begin
  5.   end;
  6.  
  7. begin
  8.   ...
  9.   SomeObject.OnEvent := @Bar;  //????
  10.   ...
  11. end;

Is it possible? This doesn't work: TMyEvent = procedure of object of object;
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5875
Re: procedure of object of object?
« Reply #1 on: February 23, 2017, 06:13:35 pm »

Code: Pascal  [Select]
  1.  {$modeswitch nestedprocvars}
  2. type teventtype = procedure is nested;

Edson

  • Hero Member
  • *****
  • Posts: 806
Re: procedure of object of object?
« Reply #2 on: February 23, 2017, 06:35:40 pm »

Code: Pascal  [Select]
  1.  {$modeswitch nestedprocvars}
  2. type teventtype = procedure is nested;

Thanks. Sometimes FPC surprises me.
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 556
Re: procedure of object of object?
« Reply #3 on: February 23, 2017, 07:36:53 pm »
Note that this is very dangerous. As soon as the outer procedure is exited, the procedural variable (which is the generic term for what you call an event handler) will become invalid. The reason is a nested procedure has access to the context of the outer procedure, and when the outer procedure exits this context gets destroyed (and hence becomes invalid).

Edson

  • Hero Member
  • *****
  • Posts: 806
Re: procedure of object of object?
« Reply #4 on: February 23, 2017, 09:44:53 pm »
OK. I see it's dangerous. Maybe only useful for events of local objects.
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus