Archived

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

MButchers

CallByName

Recommended Posts

MButchers    123
Hello there, in VB there is a function called CallByName which you can simply pass the name of an object the method to be called and the parameters to be passed is there anything simular in Delphi.(Before I write my own). Thank you.

Share this post


Link to post
Share on other sites
Sly    128
It can certainly be done, but it is not as simple as it first seems. Firstly, not all objects have a Name property. They must descend from TComponent to get the Name property or specify a Name property for each object.

Secondly, this would be a real time killer in an application, especially if it was called inside a tight loop. This is why:
  • Iterate through all known objects
    • retrieving the RTTI for the object
    • iterating through published properties to find the Name property
    • doing a string compare with the Name property
    until a match is found.
  • Iterate through all public methods of the object doing a string compare until the given method name is found.
  • Call the method.
As you can see, this would be awfully slow in an application that potentially had hundreds or thousands of objects.

Steve ''Sly'' Williams  Code Monkey  Krome Studios

Share this post


Link to post
Share on other sites