Lazarus

Free Pascal => Beginners => Topic started by: skepta on January 23, 2023, 10:52:17 am

Title: [Solved] How to get the actually instantiated class name from Abstract Class?
Post by: skepta on January 23, 2023, 10:52:17 am
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. (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!
Title: Re: How to get the actually instantiated class name from Abstract Class?
Post by: Zvoni on January 23, 2023, 11:04:45 am
Unit TypInfo?
https://www.freepascal.org/docs-html/rtl/typinfo/index-3.html
Title: Re: How to get the actually instantiated class name from Abstract Class?
Post by: cdbc 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
Title: Re: How to get the actually instantiated class name from Abstract Class?
Post by: skepta 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!
Title: Re: [Solved] How to get the actually instantiated class name from Abstract Class?
Post by: Thaddy on January 24, 2023, 02:01:08 pm
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.
TinyPortal © 2005-2018