<!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> </DIV>
<DIV>
<DIV>Janet Abdul-Karim <<A
href="mailto:jn_karim@yahoo.com">jn_karim@yahoo.com</A>> wrote</DIV></DIV>
<DIV>> <BR>> I created a subclass to ordered collections. I am
trying to add objects to the new class and then print it to make sure they are
in there. I add one object it prints but when I add another object and
print it only prints the first object i added<BR>> <BR>> sample code to
add to list.<BR>> <BR>> <STRONG>account: aAccount<BR></STRONG>>
<EM>"Add an account to the portfolio"<BR></EM>> <BR>> self
isNil ifTrue:[self add: aAccount]<BR>> ifFalse:[ self addLast:
aAccount].<BR>> <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
<STRONG>false</STRONG>. Perhaps you wanted</DIV>
<DIV>to write <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 addLast: of OrderedCollection works also when the
collection</DIV>
<DIV>is still empty. Have a lock at OrderedCollection>>add: It
calls the</DIV>
<DIV>method addLast:, nothing more! So you can simply
write</DIV>
<DIV> </DIV>
<DIV><STRONG>account: anAccount</STRONG></DIV>
<DIV> <EM>"Add an account to the portfolio"</EM></DIV>
<DIV> self add: anAccount.</DIV>
<DIV> </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> </DIV>
<DIV>> <BR>> code to print list. <BR>> <BR>> ccounts<BR>>
"Returns the accounts for the portfolio"<BR>> <BR>> self
do:[:element|^element number].<BR>> <BR>> </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 element
number.</DIV>
<DIV>This means that the execution of the do: is terminated 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> ^self collect: [:element | element number]</DIV>
<DIV> </DIV>
<DIV>the arrow returns the result of the collect expression which in turn
is</DIV>
<DIV>a collection.</DIV>
<DIV><BR>> <BR>> </DIV>
<DIV>In a separate mail, you asked for comments about this piece of code:</DIV>
<DIV>> <STRONG>findAccount: accountNumber<BR></STRONG>> <EM>"Find the
account with the argument number"<BR></EM>><BR>> self do:[:element |
element number == accountNumber<BR>> ifTrue:([Transcript show:
element;cr])<BR>> 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 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>>element is an object that has number as a method
</STRONG></DIV>
<DIV>Alright. Let us look there. I expect something like:</DIV>
<DIV> </DIV>
<DIV><STRONG>number</STRONG></DIV>
<DIV> <EM>" return the number of the account"</EM></DIV>
<DIV> ^accountNumber</DIV>
<DIV> </DIV>
<DIV>The arrow is <U>very </U>important, because it says that the</DIV>
<DIV>value of instance variable accountNumber is returned.</DIV>
<DIV>Without the arrow, the entire instance is returned. Did you</DIV>
<DIV>use the arrow?</DIV>
<DIV> </DIV>
<DIV>Additional remarks.</DIV>
<DIV>1. The method (without the arrow!)</DIV>
<DIV> </DIV>
<DIV><STRONG>number</STRONG></DIV>
<DIV> accountNumber.</DIV>
<DIV> </DIV>
<DIV>has the same meaning as<BR></DIV>
<DIV><STRONG>number </STRONG></DIV>
<DIV> accountNumber.</DIV>
<DIV> ^self</DIV>
<DIV> </DIV>
<DIV>The "^self" 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> </DIV>
<DIV>2. You wrote:</DIV>
<DIV>> 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> </DIV>
<DIV>ifTrue: [Transcript show: element;cr]<BR></DIV>
<DIV>and this is what you will see in all Smalltalk code.</DIV>
<DIV> </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> </DIV>
<DIV>With my best wishes,</DIV>
<DIV>Boris</DIV>
<DIV> </DIV></BODY></HTML>