Method Pointers

Started by
7 comments, last by Zanthos 22 years, 2 months ago
Do i have to use a class to use method pointers? or not? i''m only just learning about these(after using Delphi for a year... i disgrace even myself!) DarkStar
Advertisement
Hello there,

No you can use any procedure/function pointer wether it belong''s
to a class or not, if you are using classes however you can use

TObject.MethodAddress

this will return a pointer to the procedure / function if
if exists and is a published method.

Care should be taken if you use this method as the method returned
will have no concept of Self, in order to get around this send
the object as the first parameter of the method call and
typecast this to the actual class type in order to use methods within
the class.

Hope this makes sense

Mark
I under stood what you said, but HOW i understood it i may not be sure on :D, i'm using the iMacs :S, at college at the moment so i haven't got my code to hand, or a decent os for that matter no flames please , so i create my type for my method

    Type  M_MyOwnMethod = procedure(Sender : TObject; val : integer);  



like so, i think, and then i create this...

        Var  Samp : M_MyOwnMethod;  


and then some button click stuff and the MyMethod Test

        procedure MyMethodTest(Sender : TObject; val : integer);begin  ShowMessage('This actually works!');end;Procedure TForm1.Button1Click(Sender : TObject);begin  Samp := MyMethodTest;  Samp;end;  


It doesn't seem to work, as the only way *I* can get it to work is if i use classes...I'm using Delphi 3 btw

Edited by - zanthos on February 1, 2002 9:26:45 AM
Your code looks alright to me, with one exception. Change...
  Procedure TForm1.Button1Click(Sender : TObject);begin       Samp := MyMethodTest;       Samp;end;  

...to...
  Procedure TForm1.Button1Click(Sender : TObject);var   AnInt: integer;begin       Samp := MyMethodTest;       Samp(Sender, AnInt);end;  

That works on my computer, at least.

A slight addition to the problem, which i didn''t think would
make much difference. The question is for the TWinsock component
which some french dude made, and i was fiddling round with
method pointers, or event pointers, argh, i don''t know, thats y i''m asking, anyway, it looks like this...

  unit Unit1;interfaceuses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  StdCtrls, WSocket;type  TForm1 = class(TForm)    Button1: TButton;    WSocket1: TWSocket;    procedure Button1Click(Sender: TObject);    procedure WSocket1SessionAvailable(Sender: TObject; Error: Word);  private    { Private declarations }  public    { Public declarations }  end;var  Form1: TForm1;implementation{$R *.DFM}procedure MyMethodTest(Sender: TObject; Error: Word);begin ShowMessage(''This actually works!'');end;procedure TForm1.Button1Click(Sender: TObject);begin WSocket1.OnSessionAvailable := MyMethodTest;end;procedure TForm1.WSocket1SessionAvailable(Sender: TObject; Error: Word);begin  // Some code hereend;end.  


Why doesn''t that work? it says something about incompatible
types, thanx in advance
Hello there,

the problem lies in the way you are declaring your event handler

  the original event handler will be declared as :-type  TOnSessionAvailable = procedure(Sender: TObject; Error: Word) of object;yours is :-type  TOnSessionAvailable = procedure(Sender: TObject; Error: Word);  


spot the difference!

unfortunately you cannot specify an event handler as a non class
method as when it runs the program the ''as object'' is used to
pass the pointer to self in a register ( ebx or something ).

Hope it helps

Mark
Hello there,

the problem lies in the way you are declaring your event handler

  the original event handler will be declared as :-type  TOnSessionAvailable = procedure(Sender: TObject; Error: Word) of object;yours is :-type  TOnSessionAvailable = procedure(Sender: TObject; Error: Word);  


spot the difference!

unfortunately you cannot specify an event handler as a non class
method as when it runs the program the ''as object'' is used to
pass the pointer to self in a register ( ebx or something ).

Hope it helps

Mark
I tried using the

  of object;  


but i still got an error(now i know why ), thanx for clearing that
problem up
You need procedure MyMethodTest declare in Object decalration

type TForm1 = class(TForm)
{...}
procedure MyMethodTest(Sender: TObject; Error: Word);
private { Private declarations }
public { Public declarations }
end;

then the problem solved.

This topic is closed to new replies.

Advertisement