Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all you're doing something very unusual with <code>TList</code>: TList is a list of UNTYPED POINTERS. You can add any pointer you want to that list, and when you're doing <code>moClassList.Add( TClassA )</code> 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 <code>TClassList</code> if you actually want to add a class! Or <code>TList&lt;TClass&gt;</code> if you're using a Delphi version that support it.</p> <p>Then you're looping over the content of the list, and you're checking if <code>oObject</code> 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 <code>with oObject as TClass(moClassList[i]) do</code> you're actually casting the object to... <code>TObject</code>. Not what you wanted, I'm sure!</p> <p>And here you have an other problem: Using <code>Foo()</code> in that context will probably not work. <code>TObject</code> doesn't contain a Foo() method, but an other Foo() method might be available in context: That's the problem with the <code>with</code> keyword!</p> <p>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.</p> <hr> <p><em>Edited</em> after question clarification.</p> <p>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:</p> <pre><code>TBusinessBase = class public procedure RegisterNotification(...); procedure UnregisterNotification(...); procedure Foo;virtual;abstract; end; </code></pre> <p>In your initial example you'd no longer need the list of supported classes. You'll simply do:</p> <pre><code>oObject.Foo; </code></pre> <p>No need for type testing since Delphi is strongly typed. No need for casting since you can declare <code>oObject": TBusinessBase</code>.</p> <p>Alternatively, if you for some reason you can't change the inheritance for all your objects, you can use interfaces.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload