🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators asString
This page was created by Alexandra on 2017-08-13. Last edited by Wikiadmin on 2026-07-29.

You can use asString in OCL to obtain a string representation of any value or model object when an expression needs text.

Syntax

expression.asString

The operation is available on all objects and values. It evaluates expression and returns its string representation.

Convert a value in an expression

Use asString when you need the result as text. The conversion applies to the expression result; it does not change the type or stored value of an attribute.

For example, converting the value 123 produces a string representation of that value:

123.asString

If an object has a Date-valued attribute, use asString on that attribute when the expression requires a string:

self.SomeDate.asString

In this example, SomeDate remains a Date attribute. Only the result of self.SomeDate.asString is text.

Object string representations

When you call asString on an object, MDriven uses that object's DefaultStringRepresentation to produce the result. Define the DefaultStringRepresentation for a class when users or expressions need a consistent textual representation of its instances.

For example, if a Customer class has a DefaultStringRepresentation that describes the customer, then:

self.asString

returns that configured representation for the current Customer object.

Performance considerations

asString and DefaultStringRepresentation are not subscribed. Each use must therefore be reevaluated. This can also prevent expressions that contain asString from subscribing efficiently.

For expressions that are evaluated often, create a derived attribute such as Presentation that contains the OCL needed for the presentation text. Then use that derived attribute in DefaultStringRepresentation and refer to self.Presentation where the text is needed.

Situation Recommended approach
You need a one-time textual result from a value or object. Use expression.asString.
You need an object's configured display text. Configure and use DefaultStringRepresentation.
The text is used in a high-performance or frequently evaluated expression. Put the presentation OCL in a derived attribute, such as Presentation, rather than repeatedly using asString in the expression tree.

Related conversions and presentation syntax

asString converts a value or object result to text. It is not the same as ClassFromString, which treats a string as a model type when working with model metadata.

The <AsString> pattern used in ViewModels is a separate topic. For non-action use, that logic has been replaced by Databind labels; see Documentation:AsString.

See also