Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-12

A simple event problem in console program

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Aradeonas

Hi,

Maybe Im tierd but could any one tell me what this code wont work:

Show quoted text 19 lines
program Project1; {$mode objfpc}{$H+} uses? Classes,? SysUtils; type { Tbook } Tbook = class? private? ? fnoti: TNotifyEvent;? public? ? property noti: TNotifyEvent read fnoti write fnoti;? end; var? b: Tbook; procedure notievent(Sender: TObject);? begin? ? WriteLn('test');? end; begin? b := Tbook.Create;? b.noti := @notievent; //ERROR ? b.Free;

and give this error:

project1.lpr(30,13) Error: Incompatible types: got "<address of procedure(TObject);Register>" expected "<procedure variable type of procedure(TObject) of object;Register>"

Regards, Ara

http://www.fastmail.com - Or how I learned to stop worrying and love email again -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151215/a2d436ef/attachment.html>

Reply by email

DougC
to Aradeonas

A TNotifyEvent must be a procedure that is a method of a class. You have created and tried to use a plain procedure instead.

---- On Tue, 15 Dec 2015 17:02:48 -0500 Aradeonas &lt;&gt;wrote ----

... and give this error:

project1.lpr(30,13) Error: Incompatible types: got "&lt;address of procedure(TObject);Register&gt;" expected "&lt;procedure variable type of procedure(TObject) of object;Register&gt;"

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151215/adde04d6/attachment.html>

Reply by email

Mattias Gaertner
to Aradeonas: Maybe Im tierd but could any one tell me what this code wont work: and give this error:…
Show quoted text 14 lines
[...] Maybe Im tierd but could any one tell me what this code wont work: > program Project1; > > {$mode objfpc}{$H+} > > uses? Classes,? SysUtils; > > type > > { Tbook } > > Tbook = class? private? ? fnoti: TNotifyEvent;? public? ? property > noti: TNotifyEvent read fnoti write fnoti;? end;
Because
TNotifyEvent = procedure(Sender: TObject) of object;

The 'of object' means you need a method, not a procedure.

> project1.lpr(30,13) Error: Incompatible types: got "<address of > procedure(TObject);Register>" expected "<procedure variable type of > procedure(TObject) of object;Register>"

Mattias

Reply by email

Aradeonas
to Mattias Gaertner
A TNotifyEvent must be a procedure that is a method of a class. You have created and tried to use a plain procedure instead.

So how can I use a notify in a console program?

Because
TNotifyEvent = procedure(Sender: TObject) of object;

The 'of object' means you need a method, not a procedure. Yes I know but as you know this way we can use to notify in applications but in console I dont know. I dont write?console?programs much.

Regards, Ara

http://www.fastmail.com - Or how I learned to stop worrying and love email again -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151215/2a2697ad/attachment.html>

Reply by email

Bart
to Aradeonas: Maybe Im tierd but could any one tell me what this code wont work: and give this error:…
Aradeonas wrote
> procedure notievent(Sender: TObject); begin WriteLn('test'); end;

Create a class with a procedure notievent,simple as that.

type
  TDummy = class;
    procedure notievent(Sender: TObject);
  end;

....

var
  Dummy: TDummy;
begin
  Dummy := TDummy.Create;
  b := Tbook.Create;
  b.noti := @Dummy.notievent;
  b.Free;
  Dummy.Free;
end.

Something like that (untested code).

Or define a
type
  TNotifyProc = procedure(Sender: TObject);
(Which the will not be of type TNotifyEvent)

Bart

Reply by email

Mattias Gaertner
to Aradeonas
Thanks it works for me. But a quick question: Why we always make events like "procedure () of object" ?if we can make without "of object" ?

Quick answer: we can not use methods without "of object".

Mattias

Reply by email