Re: Delphi procedure declaration var

Posted by Pinguin

In Reply to Re: Delphi procedure declaration var posted by John, DelphiLand Team

: When using the keyword VAR in a procedure or function, you pass a variable "by reference". Such a routine can change the value of the original variable.

: Without the keyword VAR, you pass the variable "by value". The original variable can NOT be changed by the routine, including records and arrays. There's one exception: when the variable is an OBJECT, it can be changed -- but that's because in fact you are not passing the object itself but a "pointer" to it.

: Example 1: passing an array "by reference"
: [ and so on... see previous message]

Hi

Isn't it possible to force Delphi to really pass an object by value.

I have to write something like the following:

type aClass = Class
  private
    x: integer;
  public
    ....
end;

{Forward decl of some procedures not members of aClass}
  procedure DS(A, B: aClass; var C: aClass);
  ....

implementation

aClass.SomeFunction();
var
  A, B: aClass;
begin
  A.Create();
  B.Create();
  A.x := 5;
  B.x := 1;
  DS(A, B, A);
  ......
end;

procedure DS(A, B: aClass; var C: aClass);
begin
  ....
  C.SetX(0);
  ....
  C.SetX := A.GetX + B.GetX;
  ....
end;

This will only work if I can force Delphi to make a copy of A. Otherwise, when I clear C, it will also clear A :(

The only solution I got is to make the copies myself.

Follow Ups