Steve F
Steve F

Reputation: 1587

Is it possible to refer to a fields parent class

Using: Delphi XE2

A class has a field that is another class.

Is it possible in a procedure of the field to refer to the container class?

Type TClassA = class
 procedure ClassAMethod;
end;

Type TClassB = class
 ClassA : TClassA; 
end;

procedure TClassA.ClassAMethod;
begin
 // is it possible to get a reference to the 
 // owning ClassB object here?
end;

Upvotes: 3

Views: 396

Answers (1)

Rob Kennedy
Rob Kennedy

Reputation: 163357

No. There is no inherent connection between those two objects. If the contained objects needs to refer to the container, then the contained class needs to be given a reference to that object. Pass it in as a constructor parameter, for example:

constructor TClassB.Create;
begin
  inherited;
  ClassA := TClassA.Create(Self);
end;

If these objects descend from TComponent, then you might be able to use the Owner property for this.

Upvotes: 8

Related Questions