If you use var then in fact you get that these same variables are passed to the method:
SetObjectData(someObj, i); procedure SetObjectData(var AObject: TObject; var AIndex: integer); begin if someObj is TFDQuery then with (someObj as TFDQuery) do Params[0].Values[i] := DT_START; TObjectQueue.Enqueu(someObj); someObj := TObjext.Create; i := 0; end;
If you do NOT use var then copies of the variable values are transferred to the method and it looks like this:
SetObjectData(someObj, i); procedure SetObjectData(AObject: TObject; AIndex: integer); begin someObj_copy := someObj; i_copy := i; if someObj_copy is TFDQuery then with (someObj_copy as TFDQuery) do Params[0].Values[i_copy] := DT_START; TObjectQueue.Enqueu(someObj_copy); someObj_copy := TObjext.Create; i_copy := 0; end;
and, as you can see, at the end of the method, only the variables inside the method changed, and those that were passed outside remained unchanged.
But!. here is a FreeAndNil(someObj_copy) , let's say you make FreeAndNil(someObj_copy) in the method - guess what will happen? The object pointed to by both someObj_copy and someObj will be destroyed, and the pointer someObj will remain and you will get AccessViolation when you try to work with it (or worse).
varkeyword you do not get a new link. The use of this word means passing a parameter by reference , i.e. A reference is sent to the procedure to the object with which you are free to do anything. Including release and create. Entertaining Fiction . PS can also use the option given in the answer below. - Dima