Reputation: 2383
I've got TbsManager class which expose Load method like:
TbsManager = class(TComponent)
private
FItems: TbsItems;
public
procedure Load(Item: TbsItem);
The TbsItem is a TCollectionItem and it's owned by TbsItems:
TbsItem = class(TCollectionItem)
TbsItems = class(TCollection)
I want my TbsItems to have the Load method (which is in the onwer's owner class) and this is how I achieved it:
procedure TbsItem.Load;
begin
TbsManager(TbsItems(GetOwner).Owner).Load(Self);
end;
I'm not sure if I've done it right. Is it a safe code?
Upvotes: 1
Views: 304
Reputation: 613322
If your design mandates that hierarchy then your code is reasonable. I'd modify it to use checked casts, with the as
operator. These will raise runtime errors if the classes are not of the required type:
((GetOwner as TbsItems).Owner as TbsManager).Load(Self);
Upvotes: 1