Checking for discrete values
The simplest kind of data type you can add is one that specializes a data type that VA Smalltalk already supports. In this example, you will learn how to create a data type that checks for specific numeric values, such as the first five prime numbers.
Begin by creating a new nonvisual part named MyPrimeNumberConverter that inherits from the AbtIntegerConverter data type. This opens the Script Editor on the new class.
To make the new data type work, you need to create two methods. The first method, acceptsAsDisplayToObjectInput:, does the actual validation, and the second method, displayName, adds your new data type to the list of types that VA Smalltalk supports.
First, create the following public instance method:
acceptsAsDisplayToObjectInput: anInput
"Answer true if anInput is acceptable."
^(super acceptsAsDisplayToObjectInput: anInput)
and: #(1 2 3 5 7) includes: anInput asNumber]
The preceding method calls its superclass in the first statement, so that the integer data type handles most of the error checking. After it is determined that the number is a valid integer, it checks to be certain the number is one of the specific prime integers that are allowed. 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."
^'Prime Number'
After you implement displayName method, your new data type will be added to the list of converter properties in the properties table. If you forget to implement the preceding method, 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