How can I use methods as procedure pointers? 
Author Message
 How can I use methods as procedure pointers?

Hi!

This is a FPC-question: Some time ago I wrote similar to this:

{-----------------------------}
type
  EventCallBack = procedure;

procedure dosomething;
begin
  writeln('Did something.');
end;

var
  OnEvent : EventCallBack;

begin

  OnEvent;
end.
{-----------------------------}

No problem here, it worked fine. But now I like to do the same with
procedures inside classes, like that:

{-----------------------------}
type
  tEvent = procedure;

  testclass = object
        OnEvent : tEvent;
        procedure Event;
        constructor Init;
  end;

constructor testclass.Init;
begin

end;

procedure testclass.Event;
begin;
  writeln('I''m an event.');
end;

var
  tc : testclass;

begin
  tc.init;
  tc.OnEvent;
end.
{-----------------------------}

Compiler (it's FPC 1.0.6) responds:

Quote:
>Error: Method (variable) and Procedure (variable) are not compatible.

Is there just no way to use methods as procedure pointers or am I doing
something horribly wrong?

Matt



Fri, 28 Oct 2005 22:15:27 GMT  
 How can I use methods as procedure pointers?

Quote:
> No problem here, it worked fine. But now I like to do the same with
> procedures inside classes, like that:


As far as I know procedures which belong to classes could never be  
delivered as a parameter. At least it is impossible with borland/turbo  
Pascal.
but - of course - maybe I'm wrong.
--
shine on ...

      -=< Moe >=-



Fri, 28 Oct 2005 22:46:00 GMT  
 How can I use methods as procedure pointers?


Quote:
> Is there just no way to use methods as procedure pointers or am I doing
> something horribly wrong?

It is possible. You have to use the following declaration:

type
   tEvent = procedure of object;

Jonas



Fri, 28 Oct 2005 23:00:36 GMT  
 How can I use methods as procedure pointers?

Quote:
> It is possible. You have to use the following declaration:
> type
>    tEvent = procedure of object;

Interesting... is this possible with freepascal only or with borland  
pascal as well?
--
shine on ...

      -=< Moe >=-



Fri, 28 Oct 2005 23:47:00 GMT  
 How can I use methods as procedure pointers?


Quote:
> > type
> >    tEvent = procedure of object;

> Interesting... is this possible with freepascal only or with borland  
> pascal as well?

Try it :) I really don't know. At least Delphi also supports it.

Jonas



Sat, 29 Oct 2005 01:04:04 GMT  
 How can I use methods as procedure pointers?


Thanks a lot, "procedure of object;" works fine.

Matt



Sat, 29 Oct 2005 14:19:43 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. FPC: Array of method pointers or procedures

2. Help: can't get procedure pointer to work using TP 6

3. Transaction Methods in Interbase using Delphi 3.0 -Problems in commit-rollback methods

4. Passing a pointer to a procedure/function as a parameter in a procedure

5. Using a Procedure within a Procedure?

6. Pointer to method

7. It's not bad canned meat...

8. Question on passing pointer to procedure - question.zip (0/1)

9. Question on passing pointer to procedure - question.zip (0/1)

10. Pointers to procedures/functions

11. Can anyone help with pascal procedure pointers?

12. Needed: Code for array of pointers to procedures

 

 
Powered by phpBB® Forum Software