Calling member functions dynamically

Posted by user652511 on Stack Overflow See other posts from Stack Overflow or by user652511
Published on 2011-03-09T22:17:49Z Indexed on 2011/03/10 0:10 UTC
Read the original article Hit count: 142

Filed under:

I'm pretty sure it's possible to call a class and its member function dynamically in Delphi, but I can't quite seem to make it work. What am I missing?

// Here's a list of classes (some code removed for clarity)
moClassList : TList;
moClassList.Add( TClassA );
moClassList.Add( TClassB );

// Here is where I want to call an object's member function if the
// object's class is in the list:
for i := 0 to moClassList.Count - 1 do
    if oObject is TClass(moClassList[i]) then
        with oObject as TClass(moClassList[i]) do
            Foo();

I get an undeclared identifier for Foo() at compile.

Clarification/Additional Information:

What I'm trying to accomplish is to create a Change Notification system between business classes. Class A registers to be notified of changes in Class B, and the system stores a mapping of Class A -> Class B. Then, when a Class B object changes, the system will call a A.Foo() to process the change. I'd like the notification system to not require any hard-coded classes if possible. There will always be a Foo() for any class that registers for notification.

Maybe this can't be done or there's a completely different and better approach to my problem.

By the way, this is not exactly an "Observer" design pattern because it's not dealing with objects in memory. Managing changes between related persistent data seems like a standard problem to be solved, but I've not found very much discussion about it.

Again, any assistance would be greatly appreciated.

Jeff

© Stack Overflow or respective owner

Related posts about delphi