Board index » delphi » StringList := StringList ??

StringList := StringList ??

Hi!

I just read in the online help of D4 that you copy StringLists bei
assigning them with  ":=".  Is that true?  Aren't StringLists Objects?
How do I amke a shallow copy now (as the := does with normal objects)?
I'm confused, how knows the details?

cu Quarc

 

Re:StringList := StringList ??


Maybe I'm from the old school but I still like the "Assign()" method.
This makes it abundantly clear what the code is trying to do -- to copy
the content of the object from one to another.  It takes me a split
second more to type the additional 6 ASCII characters to win clarity.

Quote
>Quarc (Marc Tricou) wrote:
> I just read in the online help of D4 that you copy StringLists bei
> assigning them with  ":=".  Is that true?  Aren't StringLists Objects?
> How do I amke a shallow copy now (as the := does with normal objects)?
> I'm confused, how knows the details?

------------------------------------------------------------------
Sundial Services :: Scottsdale, AZ (USA) :: (480) 946-8259
mailto:i...@sundialservices.com  (PGP public key available.)
Quote
> Fast(!), automatic table-repair with two clicks of the mouse!
> ChimneySweep(R):  "Click click, it's fixed!" {tm}
> http://www.sundialservices.com/products/chimneysweep

Re:StringList := StringList ??


"Quarc (Marc Tricou)" <Qu...@gmx.de> skrev i en meddelelse
news:8a6kvh$4n6$1@mail.fh-wedel.de...

Quote
> I just read in the online help of D4 that you copy StringLists bei
> assigning them with  ":=".  Is that true?

I couldn't find this in the help and it would also be a huge error.
But you can copy the strings in the StringList using :=.

Quote
> Aren't StringLists Objects?

Yes.

Quote
> How do I amke a shallow copy now (as the := does with normal objects)?
> I'm confused, how knows the details?

What do you mean by "shallow copy" and "normal objects"?

You simply cannot copy objects with :=.
The only thing that would do, is to copy the object pointer, not the object.

Finn Tolderlund

Re:StringList := StringList ??


On Wed, 8 Mar 2000 23:41:56 +0100, "Quarc \(Marc Tricou\)"

Quote
<Qu...@gmx.de> wrote:
>I just read in the online help of D4 that you copy StringLists bei
>assigning them with  ":=".  Is that true?  Aren't StringLists Objects?
>How do I amke a shallow copy now (as the := does with normal objects)?
>I'm confused, how knows the details?

What is confusing you is that assigning a TStrings object (such as
TStringList) to a TStrings-type property, e.g.,

Memo1.Lines := ListBox1.Items;

copies the strings in the string lists. That's because the property's
writer method is actually calling Memo1.Lines.Assign(ListBox1.Items).

When you assign an object reference to a variable, you can copying only
the reference, and not the strings, e.g.,

var
  X, Y: TStrings;
begin
 ...
  X := Y; // now X and Y refer to the same object

Thus, if you are uncertain whether you assigning to a property or to a
variable or field, you can call Assign explicitly to copy the strings,
but usually, it is clear from context when you are assigning to a
property and when you are assigning to a variable.
--
Ray Lischner, http://www.tempest-sw.com/nutshell
author of Delphi in a Nutshell

Re:StringList := StringList ??


Quote
"Quarc (Marc Tricou)" wrote:
> Hi!

> I just read in the online help of D4 that you copy StringLists bei
> assigning them with  ":=".  Is that true?

    No, that's not true. I also don't believe you read that in the
help - what you read in the help was something about assigning
_properties_ of type TStrings.

    When you assign something to a property you are not _really_
making an assignment at all - when you assign something to a
property you are really calling the Write method for that property.
The Write method for that property can do anything whatever -
in the example you read about the Write method is calling
an Assign method.

type T = class
      procedure SetP(AValue: integer);
      function GetP: integer;
      property P: integer read GetP write SetP;
     end;

procedure T.SetP(AValue: integer);
begin
  if AValue <> 24 then
    begin
      showmessage('Invalid value');
    end
  else
    begin
      showmessage('valid value but I don''t care, I''m going to ignore
it.');
    end;
end;

function T.GetP: integer;
begin
  result:= 42;
end;

procedure TForm1.Button1Click(Sender: TObject);
var O: T;
begin
  O:= T.Create;
  try //won't do any good but you can try
    O.P:= 24;
    showmessage(inttostr(O.P));
  finally
    O.Free;
  end;
end;

Quote
> Aren't StringLists Objects?
> How do I amke a shallow copy now (as the := does with normal objects)?

    Assigning to a TStringlist _object_ will make a shallow copy. But,
for example, you there's no way to make a shallow assignment of
a stringlist to a Memo.Lines. Because AMemo.Lines is not
an object at all - it just looks like one.

Quote

> I'm confused, how knows the details?

    The details are in the chapter on Classes in the manual.
Read about properties.
Quote

> cu Quarc

Re:StringList := StringList ??


On Sat, 11 Mar 2000 01:55:24 +0100, "Quarc \(Marc Tricou\)"

Quote
<Qu...@gmx.de> wrote:
>What you all wrote is what i always thought: StringLists are normal
>obj. and Assignment with ":=" causes a shallow copy, the Assign-Method
>must be used for deep copy.

You are using the terms "shallow copy" and "deep copy" in a non-standard
fashion. Assignment of an object reference to a variables does NOT
perform a shallow copy, according to the standard definition. It copies
the reference only. Calling the Assign method performs a shallow copy:
it copies the top-level contents of the container, namely, the strings
and object references. A deep copy follows the object references and
recursively performs a deep copy of all the objects. TStrings does not
have a method to do that. Indeed, none of the standard Delphi methods
perform a deep copy.

--
Ray Lischner, http://www.tempest-sw.com/nutshell
author of Delphi in a Nutshell

Re:StringList := StringList ??


Hi!

What you all wrote is what i always thought: StringLists are normal
obj. and Assignment with ":=" causes a shallow copy, the Assign-Method
must be used for deep copy. But there is a Help Page "copying complete
Stringlists" (or similar, mine is in german) and there they say that
you can copy SL with ":=". But the examples are about Memo.lines and
ComboBix.Items. That indicates a help bug: instead of StringList they
meant TStrings, i think.
Thanks for your answers.

cu Quarc

Re:StringList := StringList ??


Hi!

Quote
> >What you all wrote is what i always thought: StringLists are normal
> >obj. and Assignment with ":=" causes a shallow copy, the
Assign-Method
> >must be used for deep copy.
> You are using the terms "shallow copy" and "deep copy" in a
non-standard
> fashion.

Yes, of course you are right. I just tried to find a short way to
desrcibe the difference between the two ways of copying. Perhaps I
should have chosen "reference" and "object" copy. Thank for
correction, my terms were misleading.

cu Quarc

Other Threads