Use the 'this' keyword for codeunit self reference

Enabled for Public preview General availability
Admins, makers, marketers, or analysts, automatically Oct 1, 2024 Oct 1, 2024

Business value

A new this keyword is introduced as known from other languages. The keyword supports passing the current object as arguments to methods, which until now has only been possible with less optimal workarounds.​ In addition, the use of the this keyword improves readability for bigger methods where it can be difficult to see if a variable is global or local scope.

Feature details

We have added support for the this keyword for self-reference on all objects, as known from other programming languages.

The main benefits of using the this keyword are that it:

  • Allows codeunits to pass a reference to this (which is itself) as an argument to another method.
  • Improves readability by signaling that a referenced symbol is a member on the object itself.

A new CodeCop rule AA0248 is added and enabled by default with severity "hidden." "Hidden" means that it shows up as three dots in the editor, but not as a diagnostics in the Problem view in Visual Studio Code, or in pipelines. There's also a workspace-wide CodeFixer to update existing code to use the this keyword.

The System Application is converted to use this for referencing methods and globals in the same object.

Tell us what you think

Help us improve Dynamics 365 Business Central by discussing ideas, providing suggestions, and giving feedback. Use the forum at https://aka.ms/bcideas.

See also

Use the 'this' keyword for codeunit self-reference (docs)