Recent

Author Topic: [Solved] How to get the actually instantiated class name from Abstract Class?  (Read 367 times)

skepta

  • Newbie
  • Posts: 2
Hi, I want to know whether it is possible to know the actual class name from the Abstract Class? For example, I have TListBox where I can add to it through the TStrings returned by Items method through the following code:

Code: Pascal  [Select][+][-]
  1. TForm1 = class(TForm)
  2.   ListBox1: TListBox;
  3. private
  4.   procedure SomeProcedure;
  5. end
  6.  
  7. procedure TForm1.SomeProcedure;
  8. begin
  9.   ListBox1.Items.Add('abc') // this works because TListBox1.Items is a TStrings
  10.   // I want to know the actual type of ListBox1.Items, for code understanding purposes
  11. end
  12.  

In this case, I know TStrings is implemented by TStringList through reading the documentation. But is there a way to know it without reading the documentation or the whole the code base? Note: this is possible in PHP through the get_class function https://www.php.net/manual/en/function.get-class.php..

In other words, I want to know what implements TStrings?

Please don't ask me to read the documentation of TStrings. Because I want to do it for other scenario where the documentation is poor/non-existent.

Thank you in advance for your help!
« Last Edit: January 24, 2023, 01:41:26 pm by skepta »

Zvoni

  • Hero Member
  • *****
  • Posts: 1678
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Sr. Member
  • ****
  • Posts: 440
    • http://www.cdbc.dk
Re: How to get the actually instantiated class name from Abstract Class?
« Reply #2 on: January 23, 2023, 11:31:43 am »
Hi
Every TObject descendant, have these class methods:
Code: Pascal  [Select][+][-]
  1. TObject = class
  2.     public
  3.     ....
  4.           class function ClassType : tclass;{$ifdef SYSTEMINLINE}inline;{$endif}
  5.           class function ClassInfo : pointer;
  6.           class function ClassName : shortstring;
  7.           class function ClassNameIs(const name : string) : boolean;
  8.    ....
  9. end;
  10.  
  11. this allows you to do stuff like this:
  12. ImplClassName:= ListBox1.Items.Classname;
  13. if ListBox1.Items.ClassType = TStringList then ...;
  14. var TypeInfo: PTypeInfo; // incl. TypInfo in uses clause
  15. TypeInfo:= ListBox1.Items.ClassInfo;
  16. if TypeInfo^.Kind = tkClass then ...;
  17.  
Hth, Regards Benny
If it ain't broke, don't fix it ;)

skepta

  • Newbie
  • Posts: 2
Re: How to get the actually instantiated class name from Abstract Class?
« Reply #3 on: January 24, 2023, 01:41:00 pm »
I think cdbc's answer is good enough and I will close this thread as solved. Thank you!

Thaddy

  • Hero Member
  • *****
  • Posts: 12933
It is much simpler:
Code: Pascal  [Select][+][-]
  1. writeln(TSomeMyClass.Classname); [/end]
  2. You can cast somemyclass to its ancestor. If this happens to be a TStrings, listboxes, memo's, comboboxes etc can be assigned to and from.
In memory of Gordon Moore  (January 3, 1929 – March 24, 2023) Just double the heaven every two years from now.

 

TinyPortal © 2005-2018