I read somewhere that reference types do not need to put var .

I have a procedure:

 procedure SetObjectData(var AObject: TObject; var AIndex: integer); begin if AObject is TFDQuery then with (Aobject as TFDQuery) do Params[0].Values[AIndex] := DT_START; TObjectQueue.Enqueu(AObject); AObject := TObjext.Create; AIndex := 0; end; 

And do I understand correctly that when I create a new object inside this procedure, I get a new link and then I need to use var ? And can rewrite this code? And then the link to the link is not very.

  • Not. When using the var keyword 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

2 answers 2

 function SetObjectData(AObject: TObject; var AIndex: integer): TObject; begin if AObject is TFDQuery then begin with (AObject as TFDQuery) do begin Params[0].Values[AIndex]:= DT_START; end; end; TObjectQueue.Enqueu(AObject); result := TObject.Create; AIndex:= 0; end; ... obj := SetObjectData(obj, i); ... 
  • Forgive me for necroposing =) but IMHO is not the answer to the question, and if you know what happens with such an action, Pointer(AObject)=@SomeOtherPointer when transmitting as in your example, please explain. I really don’t know what will happen, but the logic suggests that it will not work without var to make such a trick, i.e change the link itself - Vladimir Klykov
  • @ Vladimir Klykov Yes, it will not work. Such code will change where the function parameter points inside the function, but it will not affect the variable used as a parameter when calling the function. - Igor
  • @ Vladimir Klykov About the answer / not the answer. The author was worried about the var parameter. I showed a code variant that does the same thing, but without a var before the TObject parameter. - Igor
  • Насчет ответ/не ответ. I did not quite understand the question correctly, read it diagonally and thought out my own, the question in my head sounded like "should var be used for reference data types", so yes, you are right and you have the exact answer ... =) - Vladimir Klykov
  • obj: = SetObjectData (obj, i); Is it faster than SetObjectData (var obj, i) ;? - Artem

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).

  • one
    Oh, do not say the word "copy" in this context. Let's say "values". - Igor
  • @Igor Let it be "copies of the values." And what do you do not like "copies"? - Kromster
  • Because the expression "copy of a class instance" may be misunderstood. - Igor
  • Good. But the words "class instance" do not appear in the answer. - Kromster
  • I fully agree with @Igor, you have said копии значений переменных , Var A : TObject; Now when I pass the "copy of the value of the variable " A , I will pass the copy of the class instance , since And this is a copy of the TObject class .... - Vladimir Klykov