Archived

This topic is now archived and is closed to further replies.

Method Pointers

This topic is 5792 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

interface

uses
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 here

end;

end.


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites