Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to dynamically execute a method by name. Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Execute an object method by name 03-Mar-04
Category
VCL-General
Language
Delphi 4.x
Views
509
User Rating
9.600000
# Votes
5
Replies
0
Publisher:
Darley, F. Joe
Reference URL:
			1   
2   (* I'm not sure if this code will work in Delphi 3 but it works from 4.x on up. *)
3   
4   type
5     TForm1 = class(TForm)
6       Button1: TButton;
7       procedure Button1Click(Sender: TObject);
8       procedure SetCaption;
9     private
10      { Private declarations }
11    public
12      { Public declarations }
13      procedure DynamicExec(obj:TObject; MethodName: string) ;
14  
15    end;
16  type
17     TExec = procedure of object;
18  var
19    Form1: TForm1;
20  
21  implementation
22  
23  {$R *.dfm}
24  
25  { TForm1 }
26  
27  procedure TForm1.DynamicExec(obj: TObject; MethodName: string);
28  var
29     DynMeth: TMethod;
30     Exec: TExec;
31  begin
32     DynMeth.Data := Pointer(obj) ;  //assign pointer to object
33     DynMeth.Code := obj.MethodAddress(MethodName) ; //return address of objects 
34  method.
35     if not Assigned(DynMeth.Code) then Exit;
36     Exec := TExec(DynMeth) ;   //assign methon to Method to Method variable
37     Exec; //executes the method.
38  end;
39  
40  procedure TForm1.Button1Click(Sender: TObject);
41  begin
42     DynamicExec(Form1,'SetCaption');//call to dynamically execute method by name
43  end;
44  
45  procedure TForm1.SetCaption;
46  begin
47  caption:='Executed';
48  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC