Results 1 to 3 of 3

Thread: Event handler won't assign!?

  1. #1

    Event handler won't assign!?

    Hi! I was wondering why...

    Code:
    type
     TMyEvent = procedure(Sender: TObject) of object;
    
     TBaseClass = class
     private
      fOnClick: TMyEvent;
     public
      property OnClick: TMyEvent read fOnClick write fOnClick;
     end;
    
     {$M+}
     TDescentClass = class(TBaseClass)
     private
      fClicked: Boolean;
     public
      constructor Create;
     published
      procedure DoClick(Sender: TObject);
     end;   
     {$M-}
    
    implementation
    
     
    constructor TDescentClass.Create;
    begin
     fClicked := False;
     Onclick := DoClick(Self);
    end;
    
    procedure TDescentClass.DoClick(Sender: TObject);
    begin
     Sender.fClicked := True;
    end;
    ... this doesn't compile!?! It says: [DCC Error] Test.pas(312): E2010 Incompatible types: 'TMyEvent' and 'procedure, untyped pointer or untyped parameter'...

    ...and colors with red the line where I try to assign published method to object variable.

    If I change the Create method like this:
    Code:
    constructor TDescentClass.Create;
    var
     Method: TMethod;
    begin
     fClicked := False;
     Method.Data := Pointer(Self);
     Method.Code := MethodAddress('DoClick');
     Onclick := TMyEvent(Method);
    end;
    Then it compiles fine and method works perfectly!!

    Where might be a problem? Why I cannot assign object method to event handler?
    - In a world without the wall and fences, who will need the Gates and Windows.

  2. #2

    Re: Event handler won't assign!?

    Try changing
    Onclick := DoClick(Self);
    into
    Onclick := DoClick;

    You are supposed to pass a pointer to procedure, not result of procedure (functions give results, procedures even don't).

  3. #3

    Re: Event handler won't assign!?

    Yes, that works! Thanks User137!!

    The solution was so obvious. Maybe I had too much continous coding on behind because I didn't see it myself.

    Thanks again anyway even that the other solution was also fine for me.
    - In a world without the wall and fences, who will need the Gates and Windows.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Comodo SSL