Christopher Chase
Christopher Chase

Reputation: 2890

How can I determine whether a Delphi object is of a specific class and not any descendant class?

I have these classes and a procedure:

 TParent = class(TObject);
 TChild1 = class(TParent);     
 TChild2 = class(TParent);

 Procedure DoSomething(obj:TParent);

What I would like to do is when obj is a TParent and not a descendant raise an exception.

I thought about doing something like this:

if obj.classname = TParent.classname then raise exception.create....

but it seems a bit hackish (TM)

More: What i intended is to able to pass objects that shared properties/procedures in common. After more thought, the TParent Object isn't really needed at all, what i needed was an interface object shown in my answer.

Upvotes: 25

Views: 33593

Answers (6)

Robson Benedito
Robson Benedito

Reputation: 145

There is a reserved word (IS) to check whether a instance is a type or a child of a type.

Example:

TFruit = class
end;
TApple = class(TFruit)
end;
TOrange = class(TFruit)
end;

procedure check;
var 
   a: TApple;
   b: TOrange;
begin
   if (a is TFruit) then showmessage('a is a fruit');
   if (b is TFruit) then showmessage('b is also a fruit');
end;

Upvotes: 0

Christopher Chase
Christopher Chase

Reputation: 2890

I think ive solved what i was trying to do, It hit me on the head last night.

iParentInterface = interface(IUnknown);
TChild1 = class(TInterfacedObject,iParentInterface);     
TChild2 = class(TInterfacedObject,iParentInterface);   

Procedure DoSomething(obj:iParentInterface);

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613003

Good practice in object-oriented programming states that this should not be done. What you are describing is a direct violation of the Liskov substitution principle which states that:

objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program

I think you should explain what problem you are attempting to solve and then a better approach might become apparent.

Upvotes: 13

Uwe Raabe
Uwe Raabe

Reputation: 47714

Another approach: Introduce an abstract method in TParent, say CheckValidChild, and override it in the descendant classes. Now when you call obj.CheckValidChild you get an EAbstractError if the instance of obj is of class TParent.

Upvotes: 3

David
David

Reputation: 13580

You'll probably find the following TObject class methods useful:

  • ClassType - returns the class of an object
  • ClassParent - gives the parent class of the class
  • InheritsFrom - returns if a class inherits from another class (ie, checks the entire inheritance chain). It includes the current class.

So, you could achieve what you want (descends from TParent but not TDescendant?) with something like the following code (untested, don't have Delphi at this moment):

if obj.ClassType.InheritsFrom(TParent)
  and not obj.ClassType.InheritsFrom(TDescendant) then...

Or, if I've misunderstood and you just want to see if an object is a TParent, and not any kind of descendant at all, try:

if obj.ClassType = TParent then...

Delphi was way ahead of its time by providing access to classes via metaclasses, so rather than just checking the class name you can access an actual class object.

Upvotes: 40

Mason Wheeler
Mason Wheeler

Reputation: 84550

You're on the right track, but instead of comparing classnames, it would be simpler to check the ClassType property.

if obj.ClassType = TParent then raise exception.create....

Upvotes: 18

Related Questions