• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

object inside object

this should be an easy one.

if in an object method declaration I want to create another object to use within that method, what is the best way to do it?  Particularly with respect to the Create method of the created object - do you use 'Self' or 'Nil' or something else?  Exactly what does 'self' mean anyway?  My understanding is that a reference to 'self' within the declaration of an object method is a reference to the object itself, once it is created.  is this true?

eg:

type
  TMyObject = class(TWhatever)
  public
    procedure Execute; override;
  end;

implementation

procedure TMyObject.Execute;
var
  Other : TAnotherObject;
begin
  Other:=TAnotherObject.Create(Self);
  {now do something with Other}
  Other.Free;
end;

thanks,

Ben
0
1750
Asked:
1750
1 Solution
 
viktornetCommented:
SELF will refer in this case to TMyObject so when you pass SELF as the parameter it will replace it with TMyObject.... You use NIL as the Owner when you don't want your object to have an owner and when you want to have an owner you use its name.... In this case when you say Boject.Create(SELF) you tell the Conctructor to set TMyObject as the owner of our new object.,,,

Hope this info helps...

-Viktor
--Ivanov
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now