Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-06

Any way to find out ancestry of object? (at runtime)

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marcel van der Heide

I would like to find out at runtime if an object is a descendant of a certain object. Is there any way to do this? The TypeOf function can be used to find out if an object _is_ of a certain type but not if an object is a descendant of an object.
If there is no way to do this (easily), maybe it would be a good idea to add such a function?

Any help would be greatly appreciated.

Best wishes,

Marcel

Jonas Maebe
to Marcel van der Heide
Marcel van der Heide wrote 7 lines
I would like to find out at runtime if an object is a descendant of a certain object. Is there any way to do this? The TypeOf function can be used to find out if an object _is_ of a certain type but not if an object is a descendant of an object.

This is only available for classes (using the inheritsfrom() method), not for objects.

PS: don't use typeof() on classes, it returns bogus results there. Use the "is" operator instead for classes. The compiler still needs to be fixed so it rejects typeof() applied to classes, since it only works on TP-style objects.

Jonas

Michael Van Canneyt
to Marcel van der Heide: I would like to find out at runtime if an object is a descendant of a certain object. Is…
Marcel van der Heide wrote 6 lines
I would like to find out at runtime if an object is a descendant of a certain object. Is there any way to do this? The TypeOf function can be used to find out if an object _is_ of a certain type but not if an object is a descendant of an object. If there is no way to do this (easily), maybe it would be a good idea to add such a function?

For classes you can use the 'is' function:

If TSomeClass is  TSomeOtherClass then
  ..

I am not sure whether it is possible for objects also.

Michael.

Sebastian Günther
to Michael Van Canneyt
Michael Van Canneyt wrote 5 lines
For classes you can use the 'is' function: If TSomeClass is TSomeOtherClass then ..

or, somewhat more readable in my opinion:

  if x.InheritsFrom(y) then
     ...

- Sebastian