[Newbies] Howto initialize class variables

David T. Lewis lewis at mail.msen.com
Sat Mar 20 19:01:00 UTC 2010


On Sat, Mar 20, 2010 at 10:10:10AM +0100, Bert Freudenberg wrote:
> On 20.03.2010, at 09:46, Alex Schenkman wrote:
> > Hi List!
> > 
> > I'm playing with class variables. How should I initialize them?
> > I've tried with the class method initialize but it does not do the job.
> > 
> > Pirulo class>>initialize
> >     super initialize.
> >     myClassVar := Dictionary new at: 'one' put: 1.
> 
> * never call "super initialize" on the class side
> * class variables should be capitalized
> * class initializers need to be executed manually
> * they are only executed automatically when loading the class (e.g. file-in or via Monticello)
> 
> So this would be right:
> 
> Pirulo class>>initialize
>     "self initialize"
>     MyClassVar := Dictionary new at: 'one' put: 1.


One other note - In this example you probably intended to save the
actual dictionary in MyClassVar, in which case you would do this:

    MyClassVar := Dictionary new at: 'one' put: 1; yourself.

This is because the #at:put: message answers the thing you added,
not the dictionary, so sending the cascaded #yourself message
will answer the dictionary itself and save it in MyClassVar.

Dave

> The "self initialize" comment is conventionally put there so you can easily double-click after the opening quotes to select the whole expression, and do-it. You need to do it any time you change the initialize method. This works because when evaluating code in a Browser, "self" refers to the currently selected class.
> 
> - Bert -


More information about the Beginners mailing list