String Assignment in Object Pascal

wc...@morgan.com wrote in article <327C0DB8.4...@morgan.com>...

Quote
> Would somebody tell me whether Params[i].name still prefixes with 'OLD_'
if old test
> true after executing the following procedure, which I took from

TUpdateSQL component?

I think so, haven't tested it or read the code throughfully though.

Quote
> procedure TUpdateSQL.SetParams(UpdateKind: TUpdateKind);
> var
>   I: Integer;
>   Old: Boolean;
>   PName: string;
>   Field: TField;
> begin
>   if not Assigned(FDataSet) then Exit;
>   with Query[UpdateKind] do
>   begin
>     for I := 0 to Params.Count - 1 do
>     begin
>       PName := Params[I].Name;
>       ^^^^^^^^^^^^^^^^^^^^^^^^
>    "PName just gets the same pointer, right?"

I don't think so, PName is declared as a String, which has some "magical
power". It points to the same data until you change one of the strings,
then a copy is performed. ("Copy on Write Semantics").
I think the name PName is misleading, maybe it is short for
Param(eter)Name?
(Don't know this context, so I'm definitely not sure).

Quote

>       Old := CompareText(Copy(PName, 1, 4), 'OLD_') = 0;
>                    ^^^^^^^^^^^^^^^^^^^^^^^^
>                    "Memory leak?"

I hope it isn't. I use this kind of construct myself many places. Strings
are reference counted, and automatically released when nobody references it
anymore. (At least I think that is the theory!)

Quote
>       if Old then System.Delete(PName, 1, 4);
>                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
>               "So Params[I].Name no longer prefixes with OLD_.
>                    But this is impossible! What's going on?"

Again, I don't know this context, but I don't think Params[I].Name is
changed if you change PName.

If I have understood it right, strings are as easy to use in Delphi 2.0 as
in Visual Basic and similiar "easier" tools/languages.

Regards,

Jarle stabell