<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>&nbsp;</DIV>
<DIV>
<DIV>Janet Abdul-Karim &lt;<A 
href="mailto:jn_karim@yahoo.com">jn_karim@yahoo.com</A>&gt; wrote</DIV></DIV>
<DIV>&gt; <BR>&gt; I created a subclass to ordered collections.&nbsp; I am 
trying to add objects to the new class and then print it to make sure they are 
in there.&nbsp; I add one object it prints but when I add another object and 
print it only prints the first object i added<BR>&gt; <BR>&gt; sample code to 
add to list.<BR>&gt; <BR>&gt; <STRONG>account: aAccount<BR></STRONG>&gt; 
&nbsp;<EM>"Add an account to the portfolio"<BR></EM>&gt; <BR>&gt; &nbsp;self 
isNil ifTrue:[self add: aAccount]<BR>&gt; &nbsp; ifFalse:[ self addLast: 
aAccount].<BR>&gt; <BR>I think this is an instance method of your subclass. 
Please note that</DIV>
<DIV><STRONG>self isNil </STRONG>will always return the result&nbsp; 
<STRONG>false</STRONG>. Perhaps you wanted</DIV>
<DIV>to write&nbsp; <STRONG>self isEmpty</STRONG>, which returns <STRONG>true 
</STRONG>when the collection does</DIV>
<DIV>not contain a single element. That test is not necessary, the 
instance</DIV>
<DIV>method&nbsp; addLast: of OrderedCollection works also when the 
collection</DIV>
<DIV>is still empty. Have a lock at&nbsp; OrderedCollection&gt;&gt;add:&nbsp; It 
calls the</DIV>
<DIV>method&nbsp; addLast:, nothing more!&nbsp;So&nbsp;you can simply 
write</DIV>
<DIV>&nbsp;</DIV>
<DIV><STRONG>account: anAccount</STRONG></DIV>
<DIV>&nbsp;&nbsp;<EM>"Add an account to the portfolio"</EM></DIV>
<DIV>&nbsp;&nbsp; self add: anAccount.</DIV>
<DIV>&nbsp;</DIV>
<DIV>It is so very simple, but perhaps you saw difficulties where no</DIV>
<DIV>difficulties are? (There are other languages that require the</DIV>
<DIV>test for emptiness, you are right.)</DIV>
<DIV>&nbsp;</DIV>
<DIV>&gt; <BR>&gt; code to print list. <BR>&gt; <BR>&gt; ccounts<BR>&gt; 
&nbsp;"Returns the accounts for the portfolio"<BR>&gt; <BR>&gt; &nbsp;self 
do:[:element|^element number].<BR>&gt; &nbsp;<BR>&gt; </DIV>
<DIV>Here we have a punctuation problem: The small arrow</DIV>
<DIV>is a jump instruction. When it is executed the first time, your</DIV>
<DIV>method is <U>immediately left </U>with result value&nbsp; element 
number.</DIV>
<DIV>This means that the execution of the do: is terminated&nbsp;after </DIV>
<DIV>processing of one single element of the collection.</DIV>
<DIV>Ned proposed a better solution. Did you observe that he</DIV>
<DIV>did not only replace the <STRONG>do: </STRONG>with 
<STRONG>collect</STRONG>:, but that he also moved</DIV>
<DIV>the arrow to a different place?</DIV>
<DIV>In </DIV>
<DIV>&nbsp;&nbsp;&nbsp; ^self collect: [:element | element number]</DIV>
<DIV>&nbsp;</DIV>
<DIV>the arrow returns the result of the collect expression which in turn 
is</DIV>
<DIV>a collection.</DIV>
<DIV><BR>&gt; &nbsp;<BR>&gt; </DIV>
<DIV>In a separate mail, you asked for comments about this piece of code:</DIV>
<DIV>&gt; <STRONG>findAccount: accountNumber<BR></STRONG>&gt; <EM>"Find the 
account with the argument number"<BR></EM>&gt;<BR>&gt; self do:[:element | 
element number == accountNumber<BR>&gt; ifTrue:([Transcript show: 
element;cr])<BR>&gt; ifFalse:([Transcript show: 'No such account exists in this 
portfolio';cr])].<BR></DIV>
<DIV>Some subscribers already expressed different opinions about the 
possible</DIV>
<DIV>reason of the unexpected result.</DIV>
<DIV>I defined two small classes to try&nbsp;your and I see no problem - but 
there may</DIV>
<DIV>be problems in parts of your code that you did not post. You wrote:</DIV>
<DIV><STRONG>&gt;element is an object that has number as a method 
</STRONG></DIV>
<DIV>Alright. Let us look there. I expect something like:</DIV>
<DIV>&nbsp;</DIV>
<DIV><STRONG>number</STRONG></DIV>
<DIV>&nbsp;&nbsp; <EM>" return the number of the account"</EM></DIV>
<DIV>&nbsp; ^accountNumber</DIV>
<DIV>&nbsp;</DIV>
<DIV>The arrow is <U>very </U>important, because it says that the</DIV>
<DIV>value of&nbsp; instance variable&nbsp; accountNumber is returned.</DIV>
<DIV>Without the arrow, the entire instance is returned. Did you</DIV>
<DIV>use the arrow?</DIV>
<DIV>&nbsp;</DIV>
<DIV>Additional remarks.</DIV>
<DIV>1. The method (without the arrow!)</DIV>
<DIV>&nbsp;</DIV>
<DIV><STRONG>number</STRONG></DIV>
<DIV>&nbsp;&nbsp; accountNumber.</DIV>
<DIV>&nbsp;</DIV>
<DIV>has the same meaning as<BR></DIV>
<DIV><STRONG>number </STRONG></DIV>
<DIV>&nbsp;&nbsp;&nbsp; accountNumber.</DIV>
<DIV>&nbsp; ^self</DIV>
<DIV>&nbsp;</DIV>
<DIV>The "^self"&nbsp; causes the receiver instance to be returned.</DIV>
<DIV>When you compare the method receiver (an account) with</DIV>
<DIV>an account number, you will of course always get the value "false".</DIV>
<DIV>&nbsp;</DIV>
<DIV>2. You wrote:</DIV>
<DIV>&gt; ifTrue:([Transcript show: element;cr])<BR></DIV>
<DIV>At first sight I was surprised to see this and I was even </DIV>
<DIV>more surprised to see it work. A more careful thinking revealed</DIV>
<DIV>that it is possible to place the round parentheses around the</DIV>
<DIV>block, but they are unnecessary. You may write:</DIV>
<DIV>&nbsp;</DIV>
<DIV>ifTrue: [Transcript show: element;cr]<BR></DIV>
<DIV>and this is what you will see in all Smalltalk code.</DIV>
<DIV>&nbsp;</DIV>
<DIV>I hope that our community gives helpful advice, but</DIV>
<DIV>please do not hesitate to tell us about advice that does not</DIV>
<DIV>help. When your problems persist, we will continue to </DIV>
<DIV>think about possible solutions.</DIV>
<DIV>&nbsp;</DIV>
<DIV>With my best wishes,</DIV>
<DIV>Boris</DIV>
<DIV>&nbsp;</DIV></BODY></HTML>