🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Self
This page was created by Stephanie on 2024-05-06. Last edited by Wikiadmin on 2026-07-29.

You use self in OCL expressions to refer to the object in the current evaluation context; this page is for MDriven Designer users building model expressions and ViewModels.

What self means

self is the object currently being evaluated. Its type and value depend on where you place the expression.

For example, if a ViewModel class renders Month objects, an expression on that class evaluates once for each rendered month. In that expression, self is the current Month object.

self.Days

In this example, the expression follows the Days association from each current Month. It does not refer to the month selected by the user unless that month is also the object being evaluated.

self follows expression placement

The same expression text can produce a different result when you move it to another ViewModel class. Read self as “this object here,” where “here” is the class or OCL context that owns the expression.

Where the expression is placed What self refers to Example
A ViewModel class that renders Year The individual Year currently being rendered self.Months returns months for that year.
A nested ViewModel class that renders Month The individual Month currently being rendered self.Days returns days for that month.
A root ViewModel class The root object rendered by that class In a rooted ViewModel with one root object, self can be the same object as that class’s vCurrent_... variable.

This local behavior makes self appropriate for expressions that describe relationships within each branch of a ViewModel tree.

Choose self, vCurrent, or selfVM

ViewModel variables have different scopes. Choosing the right one affects both correctness and the amount of data a ViewModel fetches and sends.

Variable Scope Use it when Example
self The object at the location where the expression is evaluated You need data related to each object being rendered In a Year branch, use self.Months.
vCurrent_<ViewModelClass> The current user-selected object for the named ViewModel class; it is available throughout the ViewModel You need to respond to the current selection in another list Use the selected Year to filter an unrelated list.
vSelected_<ViewModelClass> The selected objects for the named ViewModel class when multi-selection is enabled An action must operate on several selected objects Apply an operation to every selected row.
selfVM The running ViewModel, not a model object You need to invoke ViewModel operations or inspect ViewModel state Call selfVM.ExecuteFetchHints(...) after filling a list programmatically.

Master-detail example

Consider a ViewModel with Year, nested Month, and nested Day classes. You want a user to select a year, then a month, and see the matching days.

Use the association from the object being rendered for normal master-detail navigation:

self.Months

Do not place vCurrent_AllYear in the Year-to-Month branch merely to obtain months for the selected year. While every Year branch is evaluated, vCurrent_AllYear has the same selected value in every branch. The server can therefore construct repeated, incorrect branches even though the user sees the selected branch as expected.

Use vCurrent_... when the list genuinely depends on a selection elsewhere, rather than on the object in the current branch. For the complete rendering explanation and the difference between sparse and full ViewModel trees, see Documentation:How to use vCurrent and “self” correctly in viewmodels.

Use self in OCL

self is also the receiver for OCL operations. For example, comparison operators describe their result in relation to self.

self <> otherObject

This evaluates to true when the object in the current context is different from otherObject.

When the current object has a subtype at runtime and you need subtype operations, use oclAsType. It statically types self as the specified type; it does not change the runtime object.

self.oclAsType(SpecializedType)

Limits and common errors

Root-level expressions require a root

Packages released in January 2025 enable stricter Require Root checks by default. To use self at root level, the ViewModel’s Requires-Root setting must be selected. This enforces that a root object exists for the expression context.

If a previously valid root-level expression now reports an error, check whether it uses self and whether Requires-Root is enabled. See Documentation:Breaking changes for the change details.

Do not use self in ViewModel context actions

In ViewModel context actions, self is undefined. Use vCurrent_<TheViewOwningTheAction> to address the current object of the ViewModel class that owns the action.

For example, replace:

self.SomeOperation()

with an expression based on the owning class’s current variable:

vCurrent_TheViewOwningTheAction.SomeOperation()

Class actions have a self context; ViewModel context actions do not.

Do not confuse the object with the ViewModel

self is a model object in the local expression context. It is not the running ViewModel. If you need to execute ViewModel functions such as fetch hints, save, refresh, or an action, use selfVM instead. The available functions are documented in Documentation:SelfVM.

For example, after programmatically adding objects to a ViewModel list, use selfVM.ExecuteFetchHints(...) when you need configured fetch hints to run for that list. See Documentation:Efficient ViewModel fetch for the full pattern.

Checklist

Before using self, verify:

  1. Which ViewModel class or OCL construct owns the expression?
  2. What object is evaluated at that location?
  3. Should the expression follow each rendered object’s associations? Use self.
  4. Should it use the user’s current selection from a named list? Use vCurrent_....
  5. Should it operate on the ViewModel itself? Use selfVM.
  6. Is the expression at root level? Ensure Requires-Root is selected.
  7. Is the expression in a ViewModel context action? Use the appropriate vCurrent_... variable instead of self.

See also