DelphiFAQ Home Search:

Testing if two objects are 'related' or 'identical' (RTTI)


commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Do you need to know whether object a is of a derived class from the class that another object b is of? Or if they may even be of the same class?

The following little code snippet tells it..

program dummy;

  b: TObject;

  // some code to assign the pointers
  // ...

  // now evaluate the RTTI of two instantiated objects
  if a is b then
    ShowMessage('a is derived from b or same class');

  if a.classtype=b.classtype then
    ShowMessage('a and b are of the same class');

  // alternative to ClassType comparison (slower!)
  if a.ClassName=b.ClassName then
    ShowMessage('a and b are of the same class')
You don't like the formatting? Check out SourceCoder then!




NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: