> When you pass an object as a procedure parameter and you don't need to
pass
> back any changes to the calling function, do you need the 'var' in front
of
> the instance name in the procedure declaration?

The actual names of the parameter passing conventions are Value and
Reference.

Using Var or Const causes a pass by reference otherwise parameters are
*always*
pass by value.

The Catch?  Delphi is lying to you...

procedure A(Obj :TObject);
begin
   Obj.Free;
   Obj := nil;
end;

procedure B(var Obj :TObject);
begin
  Obj.Free;
  Obj. := nil;
end;

var
    X :TStringList;
begin
    X := TStringList.Create;
    A(X);
    // X is not nil but the StringList doesn't exist anymore;
    X := TStringList.Create;
    B(X);
    // X is nil and the StringList doesn't exist anymore;
end;

> I routinely write procedures that receive tables and other instances that
I
> want the changes passed back into the calling code (i.e. passed by
reference
> rather than value). But since having a problem in Delphi 2 passing a
> TStringList as a value parameter into a procedure I have always routinely
> written 'var' to make the compiler pass the instance as a reference even
> though it isn't necessary to pass it back to the caller.

Passing objects as var is not particularly useful unless it's for something
like
the following...

procedure AddTenCount(var SL :TStrings);
var
    I :Integer;
begin
   if SL=nil then SL := TStringList.Create;
   for I := 1 to 10 do SL.Add(IntToStr(I));
end;

var
   A,B :TstringList;
begin
   A := TStringList.Create;
   A.LoadFromFile('C:\Spam.Txt');
   AddTenCount(A);
   AddTenCount(B);
   // Now this procedure is responsible for freeing both Objects.
   A.Free;
   B.Free;
end;

> That doesn't seem to apply in Delphi 3 as instances pass as value
parameters
> fine (the compiler presumably still passes a reference rather than the
> object itself, though)

The passing process has always been this way since D1... Note that Records,
strings
and arrays should always be passed as const or var so that delphi does not
'copy' the
data using unnecessary memory.

--
Aaron@home

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to