Re: pointer syntax



swansnow wrote:
> * If you have a non-object data structure, with "var" in the paramater
> list, you're passing by value, which means you're actually creating a
> copy of the data structure and passing that it. If you make changes to
> it, the changes get passed back, and so they persist. You can avoid
> using pointers (and their messy syntax) if you do things this way.

No, it's the opposite. 'var' means passing by reference. When used with a
pointer, it's the *pointer value* that's referenced, e.g., you could pass a
nil value in the object parameter, create an instance and assign this
instance to the object parameter - and have the reference once the routine
returns.

Example:

procedure CheckCreateStringList(var List: TStringList);
begin
if not Assigned(List) then
List:=TStringList.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
List: TStringList;
begin
CheckCreateList(List);
List.Free;
end;

I guess someone would claim that this is strange coding style, but it's
perfectly valid ;-)

--
Bjørge
bjorge@xxxxxxxxxxxx


.


Quantcast