Page 1 of 1

Domain Attribute

Posted: Fri Aug 07, 2009 11:35 am
by ConvertFromOldNGs
by ceisa >> Sat, 22 Nov 2008 10:40:56 GMT

Hallo Jade Programmer

During designing a software, I found such basic problem in making an attribute.

here is my problem/question.

1. Domain Attribute
is it possible in Jade creating a Domain Attribute ?
( Example of domain Attribute is Attribute Gender (Male or Female) or Attribute Married (yes or no) or Age ( 1 ... 120 ) etc.

thanks for your info and answer

Re: Domain Attribute

Posted: Fri Aug 07, 2009 11:35 am
by ConvertFromOldNGs
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.