RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TObject::MethodAddress Method (ShortString)

Returns the address of a class method by name.

Pascal
class function MethodAddress(const Name: ShortString): Pointer; overload;
class function MethodAddress(const Name: string): Pointer; overload;
C++
Pointer MethodAddress(const ShortString Name);
Pointer MethodAddress(const string Name);

There are situations where it is useful to be able to invoke an object method without the need to hard code the method name in advance. Call System::TObject::MethodAddress to dynamically retrieve the address of such a method by specifying the method Name as a string.  

An easy way to invoke the method is to define a procedure or function data type, such as :  

type TProc = procedure of object; 

Assign the object name, and the System::TObject::MethodAddress to a TMethod variable, such as :  

MethodVar.Data := Pointer(ObjectInstanceName); 

MethodVar.Code := ObjectInstanceName.System::TObject::MethodAddress('MethodNameString'); 

And pass this in a call to a variable of the procedure or function type : 

Proc := TProc(MethodVar); 

Proc; 

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!