Ohhhhh,
If I know it in compile time, I really will make it...
But it is just a string... How I can call only with
the string?
Main Topics
Browse All TopicsI want to trig a method from an
constant array containg a index
and the name of the method.
The problem arises becase I can
really execute the method but it
lose the self ponter. It doesn't
know to what object it belong.
What I´m doing wrong?
(I am using the MethodAddress
method of TObject to get the
pointer to the method)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ok, just from head:
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
procedure TForm1.CallMethod(Idx: Integer);
published
procedure Method1(Sender: TObject);
procedure Method2(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CallMethod(Idx: Integer);
begin
case Idx of
1: @StringGrid1.OnClick := MethodAddress('Method1');
2: @StringGrid1.OnClick := MethodAddress('Method2');
end;
end;
procedure TForm1.Button1Click(Sender
begin
CallMethod(1);
end;
procedure TForm1.Button2Click(Sender
begin
CallMethod(2);
end;
end.
type
TMethodRec = packed record
Index: Integer;
Name: string[16];
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
MethodArr: array[1..2] of TMethodRec;
procedure TForm1.CallMethod(Idx: Integer);
published
procedure Method1(Sender: TObject);
procedure Method2(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CallMethod(Idx: Integer);
begin
@StringGrid1.OnClick := MethodAddress(MethodArr[Id
end;
procedure TForm1.Button1Click(Sender
begin
CallMethod(1);
end;
procedure TForm1.Button2Click(Sender
begin
CallMethod(2);
end;
end.
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
public
MethodArr: array[1..8] of string[16];
procedure TForm1.CallMethod(Idx: Integer);
published
procedure Method1(Sender: TObject);
procedure Method2(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CallMethod(Idx: Integer);
begin
@StringGrid1.OnClick := MethodAddress(MethodArr[Id
end;
procedure TForm1.Button1Click(Sender
begin
CallMethod(1);
end;
procedure TForm1.Button2Click(Sender
begin
CallMethod(2);
end;
end.
hi esoftbg :)
my version with a little less code
procedure TForm1.Button1Click(Sender
begin
showmessage('Hello');
end;
procedure TForm1.Button2Click(Sender
Var
MyProc : procedure of object;
Begin
TMethod(myProc).data:=self
TMethod(MyProc).code :=MethodAddress('go');
if Assigned(MyProc) Then
MyProc;
TMethod(MyProc).code :=MethodAddress('Button1Cl
if Assigned(MyProc) Then
MyProc;
end;
procedure TForm1.go; //in published
begin
SHowMEssage('I''m going!!!');
end;
esoftbg:
I'm already got to something similar to
your solution. But had you tried to change
button captions on that called methods?
Doesn't work, delphi throws a exception.
Which get us to real solution...
Ferrucio68:
Great work dude!!!! It solved the
problem of self-referencing!!!
In eosftbg examples (and my ones too)
I cannot change the button captions (or other operations involving the
form members)
inside the called method -- can be workarounded
including "Self." before all instructions... But it means to change
code, what I DON'T want.
But you solved! The TMethod thing solved
the problem.
Well, how to solve this point distribution? Well, I'll increase the
question to 350 and give 100 to eosftbg and the rest to Ferrucio68.
Comment if you approve that.
This is tested and works: after Button1 click Method1 is assigned as StringGrid1Click, but after Button2 click Method2 is assigned as StringGrid1Click.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private{ Private declarations }
public { Public declarations }
MethodArr: array[1..8] of string[16];
procedure CallMethod(Idx: Integer);
published
procedure Method1(Sender: TObject);
procedure Method2(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
MethodArr[1] := 'Method1';
MethodArr[2] := 'Method2';
end;
procedure TForm1.Method1(Sender: TObject);
begin
ShowMessage('It was just called Method1');
end;
procedure TForm1.Method2(Sender: TObject);
begin
ShowMessage('It was just called Method2');
end;
procedure TForm1.CallMethod(Idx: Integer);
begin
@StringGrid1.OnClick := MethodAddress(MethodArr[Id
end;
procedure TForm1.Button1Click(Sender
begin
CallMethod(1);
end;
procedure TForm1.Button2Click(Sender
begin
CallMethod(2);
end;
end.
eosftbg:
Friend, try modifying Method2 to:
procedure TForm1.Method2(Sender: TObject);
begin
Button2.Caption := 'It was just called Method2';
ShowMessage('It was just called Method2');
end;
My D6 does not allow it, throws a exception,
except if I change it to:
procedure TForm1.Method2(Sender: TObject);
begin
Self.Button2.Caption := 'It was just called Method2';
ShowMessage('It was just called Method2');
end;
But if done in Ferrucio68's way, you can call
Method2 without worrying about changing it
a line!!!!
Just to complete the answer to earn correctly these points :)
It could be written as a function....
Function TForm1.ExecMethod(MethodNa
Var
MyProc : procedure of object;
Begin
Result := False
TMethod(myProc).data:=self
TMethod(MyProc).code :=MethodAddress(MethodName
if Assigned(MyProc) Then begin
Result := True;
MyProc;
end;
end;
Thanks again for points and grading :)
F68 ;-)
Thanks for the points. I found that it is a must an original method /procedure StringGrid1Click(Sender: TObject);/ to be existed on design time. Then on run-time it can be replaced with other method /Method1, Method2..../ correctly. I don't know why ?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure StringGrid1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private{ Private declarations }
public { Public declarations }
MethodArr: array[1..8] of string[16];
procedure AssignMethod(Idx: Integer);
published
procedure Method1(Sender: TObject);
procedure Method2(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
MethodArr[1] := 'Method1';
MethodArr[2] := 'Method2';
end;
procedure TForm1.StringGrid1Click(Se
begin
ShowMessage('It is the Original Method');
end;
procedure TForm1.Method1(Sender: TObject);
begin
Button2.Caption := 'Button 2';
Button1.Caption := 'It was just called Method1';
Edit1.Text := 'It was just called Method1';
ShowMessage('It was just called Method1');
end;
procedure TForm1.Method2(Sender: TObject);
begin
Button1.Caption := 'Button 1';
Button2.Caption := 'It was just called Method2';
Edit1.Text := 'It was just called Method2';
ShowMessage('It was just called Method2');
end;
procedure TForm1.AssignMethod(Idx: Integer);
begin
@StringGrid1.OnClick := MethodAddress(MethodArr[Id
Edit1.Text := 'It was just assigned Method' + IntToStr(Idx);
end;
procedure TForm1.Button1Click(Sender
begin
AssignMethod(1);
end;
procedure TForm1.Button2Click(Sender
begin
AssignMethod(2);
end;
end.
Business Accounts
Answer for Membership
by: esoftbgPosted on 2004-06-08 at 13:11:02ID: 11263840
The usually way to be called a method is by name: so if you know its name just call it by name....