In the Public Interface Editor, from the File menu select
Generate Default Scripts. The following window
displays:
Because none of the methods exist yet, select Generate All. After VisualAge generates the code for the methods, switch to the Script Editor to see the code.
First, notice the class definition for the part is as follows:
AbtAppBldrPart subclass: #MyTimer instanceVariableNames: 'repeat length' classVariableNames: '' poolDictionaries: ''
Two instance variables have been added for you: repeat and length. The MyTimer class is a subclass of AbtAppBldrPart. AbtAppBldrPart provides all of the general properties and behaviors that a timer part needs.
Select the Not categorized category. In the method list, you see length, length:, repeat, repeat:, start, and stop. Select length to see the code that was generated for you.
length "Return the value of length." ^length
Before an instance variable has been set, its value is nil. A popular technique for initializing instance variables is lazy initialization, where the value is set the first time the instance variable is referenced. To use lazy initialization, add one line to the length method so it looks like the following:
length "Return the value of length." length == nil ifTrue: [self length: 0]. ^length
The preceding initialization technique is a convenient way to prevent errors, and is generally easier than checking for nil values each time you reference the attribute. Follow the same steps for the repeat method, using false as the initial value, as follows:
repeat == nil ifTrue: [self repeat: false].
The set methods do not need to be changed. To understand what they do, let's examine the code that was generated for the length: method:
length: anInteger "Save the value of length." length := anInteger. self signalEvent: #length with: anInteger
The preceding method simply stores a new value into the variable, and tells VisualAge that the value has been changed. Then VisualAge can notify other parts of the new value.
For the timer's length and repeat attributes, you entered both get and set selector names in the Public Interface Editor. Usually, get and set selectors come in pairs.
The exception is when an attribute is read-only. If you do not want other parts to be able to set the value of an attribute, you should enter only the get selector. When the set selector is not defined, VisualAge cannot set the attribute's value.