Jeff Wilson
Jeff Wilson

Reputation: 331

Calling member functions dynamically

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

Upvotes: 1

Views: 765

Answers (3)

Cosmin Prund
Cosmin Prund

Reputation: 25678

First of all you're doing something very unusual with TList: TList is a list of UNTYPED POINTERS. You can add any pointer you want to that list, and when you're doing moClassList.Add( TClassA ) you're actually adding a reference to the class TClassA to the list. Technically that's not wrong, it's just very unusual: I'd expect to see TClassList if you actually want to add a class! Or TList<TClass> if you're using a Delphi version that support it.

Then you're looping over the content of the list, and you're checking if oObject is of the type in the list. So you do want classes in that list after all. The test will work properly and test rather the object is of that type, but then when you do with oObject as TClass(moClassList[i]) do you're actually casting the object to... TObject. Not what you wanted, I'm sure!

And here you have an other problem: Using Foo() in that context will probably not work. TObject doesn't contain a Foo() method, but an other Foo() method might be available in context: That's the problem with the with keyword!

And to finally answer the question in the title bar: Delphi is not an Dynamic language. The compiler can't call a method it doesn't know about at compile time. You'll need to find a OOP way of expressing what you want (using simple inheritance or interfaces), or you may call the function using RTTI.


Edited after question clarification.

All your business classes need to implement some kind of notification request management, so your design benefits allot from a base class. Declare a base class that implements all you need, then derive all your business classes from it:

TBusinessBase = class
public
  procedure RegisterNotification(...);
  procedure UnregisterNotification(...);

  procedure Foo;virtual;abstract;
end;

In your initial example you'd no longer need the list of supported classes. You'll simply do:

oObject.Foo;

No need for type testing since Delphi is strongly typed. No need for casting since you can declare oObject": TBusinessBase.

Alternatively, if you for some reason you can't change the inheritance for all your objects, you can use interfaces.

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 613481

TClass is defined:

TClass = class of TObject;

You then write oObject as TClass which is effectively a null operation since oObject already was a TObject.

What you need is something like this:

type
  TFoo = class
    procedure Foo();
  end;
  TFooClass = class of TFoo;
  TBar = class(TFoo)
    procedure Bar();
  end;

....

  if oObject is TFooClass(moClassList[i]) then
    with oObject as TFooClass(moClassList[i]) do
      Foo();

This explains why your attempts to call Foo() does not compile, but I simply have no idea what you are trying to achieve. Even after your clarification I'm struggling to understand the problem.

Upvotes: 2

Ken White
Ken White

Reputation: 125748

Here's a really contrived example (using an array instead of a TList) that I think is what you're trying to do (error handling and try..finally intentionally omitted for clarity).

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TBaseClass=class(TObject)
    procedure Foo; virtual;
  end;

  TClassA=class(TBaseClass)
    procedure Foo; override;
  end;

  TClassB=class(TBaseClass)
    procedure Foo; override;
  end;

  TClassArray= array of TBaseClass;

{ TClassB }

procedure TClassB.Foo;
begin
  Writeln('TClassB.Foo() called.');
end;

{ TClassA }

procedure TClassA.Foo;
begin
  Writeln('TClassA.Foo() called.');
end;

var
  Base: TBaseClass;
  ClassArr: TClassArray;

{ TBaseClass }

procedure TBaseClass.Foo;
begin
  Writeln('TBaseClass.Foo called!!!!!!!!');
end;

begin
  ClassArr := TClassArray.Create(TClassA.Create, TClassB.Create);
  for Base in ClassArr do
    Base.Foo;

  for Base in ClassArr do
    Base.Free;
  ReadLn;
end.

Upvotes: 0

Related Questions