Sign in Join

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

Mailing lists / fpc-pascal / 2004-06

Message methods - DefaultHandler

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Mr. Ego

Hello,
I have some problem with DefaultHandler method. When I pass a message for which is special handler defined everything is ok, but when it should be passed to DefaultHandler (which is overriden) it does nothing. Here is some sample code:

{$MODE OBJFPC}
type TMyMessage = record
       MSGID : cardinal;
       text : string;
       end;
     TTest=class(TObject)
       constructor Init;
       procedure Handle (var MyMessage : TMyMessage); Message 1; virtual;
       procedure DefaultHandler(var MyMessage : TMyMessage); virtual;
       destructor Done;
       end;
constructor TTest.Init;
begin
end;
procedure TTest.Handle (var MyMessage : TMyMessage);
begin
  writeln(MyMessage.text);
end;
procedure TTest.DefaultHandler(var MyMessage : TMyMessage);
begin
  writeln(MyMessage.Text);
end;
destructor TTest.Done;
begin
end;
var MyMessage : TMyMessage;
    Moje : TTest;
begin
Moje:=TTest.Init;
MyMessage.MSGID:=1;
MyMessage.text:='Hello, world';
Moje.Dispatch(MyMessage); {this is handled by TTest.Handle method}
MyMessage.MSGID:=2;
Moje.Dispatch(MyMessage); {this should be handled by DefaultHandler, but it isn't}
Moje.Done;
end.

It should write two times the "Hello world message". For the first time from method TTest.Handle, and for the second time from method TTest.DefaultHandler. But TTest.DeafultHandler doesn't do anything (it writes only one message).

Could anybody help me, please?

Pavel B.

Thomas Schatzl
to Mr. Ego

Hello,

Show quoted text 19 lines
I have some problem with DefaultHandler method. When I pass a message for which is special handler defined everything is ok, but when it should be passed to DefaultHandler (which is overriden) it does nothing. Here is some sample code: {$MODE OBJFPC} type TMyMessage = record MSGID : cardinal; text : string; end; TTest=class(TObject) constructor Init; procedure Handle (var MyMessage : TMyMessage); Message 1; virtual; procedure DefaultHandler(var MyMessage : TMyMessage); virtual; destructor Done; end; It should write two times the "Hello world message". For the first time

from

method TTest.Handle, and for the second time from method TTest.DefaultHandler. But TTest.DeafultHandler doesn't do anything (it writes only one message).

The DefaultHandler method has a wrong signature and so your method is not called. You have to override

    procedure Dispatch(var Message); override;

and use a typecast in the method implementation, e.g.

procedure TTest.DefaultHandler(var MyMessage);
begin
  writeln(TMyMessage(MyMessage).Text);
end;

Note that Delphi (don't have FPC installed on this machine, sorry) complains that message methods may not be declared as virtual (doesn't make much sense imo, since dispatching a message to an object always is "bound" dynamically using the message identifier).

Hth,
Thomas

Peter Vreman
to Mr. Ego: I have some problem with DefaultHandler method. When I pass a message for which is…
Show quoted text 5 lines
Hello, I have some problem with DefaultHandler method. When I pass a message for which is special handler defined everything is ok, but when it should be passed to DefaultHandler (which is overriden) it does nothing. Here is some sample code:

You need to use 1.9.x. It also gives a warning why the code does not work:

p.pp(10,18) Warning: An inherited method is hidden by
"TTest.DefaultHandler(var TMyMessage)"