🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators maxLength
This page was created by Lars.olofsson on 2020-06-29. Last edited by Wikiadmin on 2026-07-29.

You can use maxLength in OCL to read the maximum permitted length of a string attribute and prevent values that are too long from being saved.

Syntax

<attribute>.maxLength

The operator returns the maximum length allowed for the string attribute.

Example

Given a class Car with the string attribute Name, use:

self.Name.maxLength

This expression returns the maximum allowed length for Car.Name.

Prevent a string attribute from overflowing

Use maxLength when code may assign text that exceeds the storage limit of an attribute. For example, the following expression truncates errorinfo before it is saved:

self.errorinfo.subString(1,self.errorinfo.maxLength-1)

This pattern helps avoid database errors such as String or binary data would be truncated. For the full guidance on diagnosing the error, choosing an appropriate attribute length, and applying truncation, see Documentation:String attribute overflowing.

Related string operators

Do not use maxLength to determine how many characters a string currently contains. Use length or Size for the current character count. Use maxLength when you need the configured maximum for a string attribute.

See also