Problems with Collection again.

Roel Wuyts roel.wuyts at iam.unibe.ch
Tue Feb 4 07:40:32 UTC 2003


On Tuesday, February 4, 2003, at 02:15 AM, Janet Abdul-Karim wrote:

> I made an instance of collection and trying to print out what is in 
> the collection.  The way I coded seems to only print out the last 
> obect that I added.
>
>  
>
> code
>
> portfolio: aAccount
>
> portfolio:= SortedCollection new.
>
> portfolio sortBlock:[:x :y | x name < y name].
>
> portfolio add: aAccount.

Warning: When you step through this piece of code, you will see that 
the result of the #add: is the aAccount you just added. So at least 
make sure that you did not write:
portfolio := portfolio add: aAccount.
Because then you will just have assigned aAccount to portfolio. Step 
through this using the debugger to see what exactly happens. I am quite 
sure that you have this somewhere in your code, and that when you print 
what you think is the portfolio, you are actually printing a single 
instance of an account. Again, I stress this (see my previous post): 
Use the debugger and step through the code. You are programming in the 
dark here. The best way to understand what the system does is by 
looking to it through the debugger.

>
> code to print portfolio
>

since you assigned an instance of SortedCollection to portflio, it will 
never be nil. So it will never come in the ifTrue: branch. You should 
use isEmpty here. I do not have your code, but I strongly believe that 
once you get to this part, portfolio is actually an instance of 
account. Since your account probably does not implement isEmpty (and 
Object does not understand it) you get an 'unhandled exception'. It 
seems to work with #isNil (i.e. you do not get an unhandled exception) 
because Object implements it. But it does not do what you want.

So: Use the debugger to put breakpoints and step through your code. And 
when you get an unhandled exception, click on the 'debug' button and 
see what actually happens. That way you can easily find out whether my 
guess is correct by inspecting the value of the instance variables.

> portfolio isNil ifTrue:[Transcript show: 'Portfolio empty'].
>
> ifFalse:[portfolio do:[:element | Transcript show: element name]].
>
> ***if you are wondering y i did not use isEmpty instead I tried and it 
> was giving me errors turned out isNil worked just fine.  That fact 
> that I could not use isEmpty make me wonder is porfolio is really a 
> collection.***
>
>  
>
>
>
<image.tiff>
>
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now
>
Roel Wuyts                                                   Software 
Composition Group
roel.wuyts at iam.unibe.ch                       University of Bern, 
Switzerland
http://www.iam.unibe.ch/~wuyts/
Board Member of the European Smalltalk User Group: www.esug.org



More information about the Squeak-dev mailing list