The default value can be of two types:
- Constant: a constant of the same data type as the attribute;
- Expression: a value calculated from an expression.
Set a constant default value #
Right-click on a native attribute to open its Attribute menu
; select the Constant
option to open the Value editor
and type in the desired default value.
Set a calculated default value #
Right-click on a native attribute to open its Attribute menu
; select the Expression
option to open the Edit default expression
panel.
In the Edit default expression
panel you can define when to calculate the attribute value using the expression:
When creating a new object
: the default value is calculated when creating a new object.When editing an existing object
: the default value is calculated every time an object is modified.
Combining the options of the other checkboxes of the editor it is possible to impose conditions on the responsiveness of the expression:
Initialize with computed value
: sets the object creation form with the value defined by the expression already entered;- Reset with computed value: every time the object is modified, the value of the attribute is recalculated based on the expression defined in the editor;
Update automatically (unless set by User)
: when creating and editing, the attribute value can also be updated automatically, unless the user manually overwrites the calculated default value.
Remove a default value #
Right-click on a native attribute to open its Attribute menu
and select the Remove default
option.
Example #
In the example shown in the figure, we modeled an online order class (Order); the date
attribute keeps track of when an order was created.
We would like to avoid the user having to fill in the date field every time (note that the field is required), so it makes sense to assign a default value to it. If the date had always been the same we could have used a constant; however, in our case the creation date literally changes day by day, so using an expression is more appropriate. In fact, since the date of the order is almost certainly the same as that of the object creation within the class (i.e. today’s date), the date
attribute is automatically computed by exploiting the __System.date
property.
Checking the option Update automatically (unless set by user)
, in the section _When creating a new object
the date will be calculated automatically by the application, without the need to enter it manually. The user is free to modify the date as he likes; as a precaution, we have inserted a message that warns him if he is inserting a date different from the default one.
In case of subsequent edits, we do not want the date to be recalculated and changed; therefore we left the settings in the When editing an existing object
section unchanged.