Hi, Herbert<br><br>Thank you very much. I got it. I referred to SBE(both smalltalk by example and squeak by example :-)).<br><br>the reason why I want to copy is because I don&#39;t want the element stored in collection to change when I modify temp var later.
<br>and because the element has other collections as instance vars, so i am not sure if I need deep copy.<br><br>I tried something like this:<br><br>coll add: (temp deepCopy).<br><br>...modify temp ex. temp:= MyItem new ...
<br><br>temp := (coll at: 1)&nbsp; deepCopy.<br><br>The above code works when there are 2 levels of&nbsp; collection.<br><br>Thanks again and best regards.<br>-Xinyu<br><br><div class="gmail_quote">On Jan 1, 2008 11:19 PM, Herbert König &lt;
<a href="mailto:herbertkoenig@gmx.net">herbertkoenig@gmx.net</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hello Xinyu,<br>
<br><br>if you come from another programming language, a collection stores<br>only pointers to objects. No chance (and no need) to copy an object<br>into a collection.<br><br>XL&gt; Object subclass: #MyItem<br>XL&gt;  instanceVariableNames: &#39;subItems&#39;
<br>XL&gt;  classVariableNames: &#39;&#39;<br>XL&gt;  poolDictionaries: &#39;&#39;<br>XL&gt;  category: &#39;MyTest&#39;<br><br>XL&gt; But I don&#39;t know how to store/get such kind of MyItem by<br>XL&gt; using collection, I think I need deep copy an element when add it
<br>XL&gt; to the collection.<br>No imho, see below.<br><br>XL&gt; I don&#39;t think the following code works.<br><br>XL&gt; |temp coll|<br>XL&gt; temp := MyItem new.<br>XL&gt; temp addSubItem: &#39;hello&#39;; addSubItem &#39;world&#39;; yourself.
<br>XL&gt; coll := OrderedCollection new.<br>XL&gt; coll add: temp<br><br>assuming subItems is initialised as:<br>OrderedCollection new<br>and addSubItem: is defined as:<br>subItems add: anObject<br>the code should work.<br>
<br><br>XL&gt; How can I add the deep copied element to my collection and<br>XL&gt; retrieve it later? shall I overload the #copy method?<br><br>The main question is: why would you want to copy the element?<br>Each MyItem holds to its collection of subItems and coll holds to your
<br>temp.<br><br>There may be reasons you want to use a copy put usually you just put<br>your objects into the collection and retrieve them with coll at:<br><br>If you really want to store a copy you could:<br>temp1 := temp deepCopy.
<br>coll add: temp1.<br><br>BTW, do you know of the free Smalltalk books, Squeak by Example being<br>the latest and specifically tailored to Squeak?<br><br><br>XL&gt; Best regards and thanks<br>XL&gt; -Xinyu<br><br>Cheers
<br><br>Herbert &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mailto:<a href="mailto:herbertkoenig@gmx.net">herbertkoenig@gmx.net</a><br><br>_______________________________________________<br>Beginners mailing list<br><a href="mailto:Beginners@lists.squeakfoundation.org">
Beginners@lists.squeakfoundation.org</a><br><a href="http://lists.squeakfoundation.org/mailman/listinfo/beginners" target="_blank">http://lists.squeakfoundation.org/mailman/listinfo/beginners</a><br></blockquote></div><br>