Postby ConvertFromOldNGs » Fri Aug 07, 2009 11:35 am
by Dean Cooper >> Sat, 22 Nov 2008 16:48:55 GMT
JADE does not have enumerated types as per your description. However, you can enforce domain values by using mapping methods.
In JADE, if you create a method with the same name as a property, it will be created as a mapping method. Mapping methods have a specific signature, where the first parameter tells you if the property is being set or read, and the second parameter holds the property value (it will be passed into the mapping method if the property is being set, otherwise it will be returned from the mapping method). Mapping methods are called whenever a property is set or read. When a property is being set, the mapping method can change the _value parameter to change the value that is assigned to the property (for example, you might want to enforce that a property is always lower case). When a property is being read, the mapping method can change the _value parameter to return a different value to that which is stored on the object, if required.
For example, say you have a property called "gender" of type Character. Assuming you have two constants (either class-level or global) called Male (value 'm') and Female (value 'f'), you could write a "gender" mapping method like this:
gender(set : Boolean; _value : Character io) mapping;begin
if set then
// property is being set
if _value <> Male and _value <> Female then
// raise an exception because the value is invalid
// error number and message could also be constants, etc
NormalException.raise_(10000, "Invalid gender");
endif;
else
// property is being read - do nothing in this case
endif;
end;
Dean.