Checking for even integers
The simplest kind of data type you can add is one that specializes one of the data types VA Smalltalk already supports. For example, you might want to check for specific numeric values or only allow even integers in an entry field.
To create an even integer data type, begin by creating a new part that inherits from the integer data type part:
1. In the Organizer, from the basic Parts menu, select New.
2. Type EvenIntegerConverter in the Part class field.
3. Select Nonvisual part.
4. Type AbtIntegerConverter in the Inherits from field.
5. Select OK.
This opens the script editor on a new part called EvenIntegerConverter. To make the new data type work, you need to create two scripts. The first script, acceptsAsDisplayToObjectInput:, does the actual validation, and the second script, displayName, adds your new data type to the list of types that VA Smalltalk supports.
First, create the following public instance script:
acceptsAsDisplayToObjectInput: anInput
"Answer true if anInput is acceptable."
^(super acceptsAsDisplayToObjectInput: anInput) and: anInput asNumber even]
This script calls its superclass in the first statement, so that the integer data type handles most of the error checking. After the number is determined to be a valid integer, it is checked further to be certain it is an even integer. Sending the asNumber message in the last line is safe, because the input has already passed the basic integer validation.
Next, create the following public class method:
displayName
"Answer a name for this class."
^'Even Number'
After you implement the displayName script, your new data type will be added to the list of data types in the Converter window. If you forget to implement this script, your data type will inherit Integer as its display name. Because VA Smalltalk already has Integer as one of its types, your new type would be ignored.
Last modified date: 01/29/2015