0

I have a tDictionary of variant items, I can add anything I wish, including objects.

My app has a "render" function that iterates through the dictionary and converts whatever it finds into string and finally displays them all as a single document.

I also have a number of classes that ALL have a "render" function that returns a string. I can add the class objects to my main dictionary if I explicitly render them: myDict['foo'] := mySubClass.render();

What I want is to add the class object: myDict['foo'] := mySubClass;

When the main render operation discovers an object, it should call the "render" function of that object. Similarly, if said object also contains objects, they should be rendered in turn. Like a coiled spring unwinding.

In PHP I simply used "if(is_object($itemArr[idx]) then $itemArr[idx]->render()"

I can't do this in Delphi, because the IDE does not know what the object is at design time.

How can I resolve this?

edited to show that I don't want to itemise the classes.

thrutch
  • 13
  • 6
  • 1
    Use the [`is` operator](https://docwiki.embarcadero.com/RADStudio/Sydney/en/Class_References#The_is_Operator) as in `if Variable is Type then`, where the variable is the object and the type is the class. This is as basic as your PHP approach, tho. – AmigoJack May 07 '22 at 09:32
  • Does that mean I would have to ask if v = class1, if v= class2, if v=class3 ...... – thrutch May 07 '22 at 09:35
  • If all those classes have no common ancestor: yes. That's where [inheritance in OOP](https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)) makes things easier. – AmigoJack May 07 '22 at 09:38
  • OK - if I can make that work, you get the point :) – thrutch May 07 '22 at 09:43
  • This also includes polymorphism! – Delphi Coder May 07 '22 at 11:00
  • Sounds like interfaces would be the right approach here. Your dict should have values that implement an interface that defines the functionality you need. – David Heffernan May 07 '22 at 11:50

0 Answers0