Share via


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

The new this keyword works like the this keyword in other languages. With the keyword, you can pass the current object as an argument to methods. Before, you had to use less optimal workarounds to pass the current object. When you use the this keyword, your code is easier to read, especially in larger methods where it might be hard to tell if a variable is in global or local scope.

Feature details

The this keyword provides self-reference support for 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 (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 the rule shows up as three dots in the editor, but not as a diagnostic in the Problem view in Visual Studio Code, or in pipelines. A workspace-wide CodeFixer updates existing code to use the this keyword.

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

Tell us what you think

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

Additional resources

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