Mega Search
23.2 Million


Sign Up

Make a donation  
Passing objects to functions  
News Group: embarcadero.public.delphi.oodesign

I am having some trouble passing an Object to a function. The Object is quite complex: It has loads of Properties, subobjects, sub-sub Object, and lists of Objects. Inside the function, I do this:

Function SomeFunction(Origin: ComplexObject): ComplexObject;
var
    NewObj: ComplexObject;
begin
    If Origin = nil Then
    Begin
        NewObj:= ComplexObject.Create;
    End
    Else
        NewObj:= Origin;
    End;
    // Edit Properties of NewObj....
    Result:= NewObj;
end;

Now, any Properties i changed on NewObj is reflected to the Origin Object i passed in. So after this function, when I want to see if some of the Properties have ACTUALLY changed and i compare NewObj to Origin, they are always Equal, because Origin has been updated aswell. How can I avoid this, and get a result as I would With VB .Nets ByVal?

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 8-Apr-2014, at 5:02 AM EST
From: Jostein Torseter
 
Re: Passing objects to functions  
News Group: embarcadero.public.delphi.oodesign
{code}
Function SomeFunction(Origin: ComplexObject): ComplexObject;
 var
     NewObj: ComplexObject;
 begin
     If Origin = nil Then
     Begin
         NewObj:= ComplexObject.Create;
         // Edit Properties of NewObj....
         //Now you edit the NewObj only
     End Else NewObj:= Origin;
     
     //problem in you posted code.. end without begin..
     End;

     // Edit Properties of NewObj....
    //problem here ??
     //Now you edit the Origin or the NewObj

     Result:= NewObj;
 end;
{code}

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 8-Apr-2014, at 6:18 AM EST
From: Robert Triest