Business Central 2024 wave 2 (BC25): Use the ‘this’ keyword for codeunit self reference

Dynamics 365 Business Central

Hi, Readers.
The public preview for Dynamics 365 Business Central 2024 release wave 2 (BC25) is available. Learn more: Link.

I will continue to test and share some new features that I hope will be helpful.

Use the ‘this’ keyword for codeunit self reference:

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.

https://learn.microsoft.com/en-us/dynamics365/release-plan/2024wave2/smb/dynamics365-business-central/use-this-keyword-codeunit-self-reference?wt.mc_id=DX-MVP-5004336

This is also mentioned in AL Language extension changelog Version 14.0. As in Feature details, it refers to all objects, not just codeunit.

We have added support for the this keyword for self-reference on all objects. The main benefits are

  • Allow codeunits to pass a reference to this as an argument to another method.
  • Improve readability by signaling that a referenced symbol is a member on the object itself.

We have added a CodeCop rule AA0248 that is 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 or in pipelines. There is also a CodeFixer to add the this keyword.

PS: About this update first came from Steffen Balslev, Source:

Let’s see more details.
With this wave (BC25), Microsoft has added support for the this keyword for self-reference.
For example, in Codeunit:

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.

This is similar to the example given by Steffen. The following is a standard example.
codeunit 205 “Alt. Cust. VAT Reg. Doc. Impl.” implements “Alt. Cust. VAT Reg. Doc.”: Session.BindSubscription(Codeunit) Method and Session.UnbindSubscription(Codeunit) Method

PS: The interface declares an interface name along with its methods, and codeunits that implement the interface methods, must use the implements keyword along with one or more interface names. The interface itself doesn’t contain any code, only signatures, and can’t itself be called from code, but must be implemented by other objects. More details: Interfaces in AL

This is also available in normal codeunit.

  • Improves readability by signaling that a referenced symbol is a member on the object itself.

For example, in the following example, all methods and OnRun (Codeunit) Trigger in the current codeunit can be accessed through the this keyword.

This is also used in the standard.
codeunit 1932 “Scheduled Perf. Profiler Impl.”:

Methods:

In addition, this is not limited to codeunit. For example, in the standard example below, it can also be used in Page.
page 1932 “Perf. Profiler Schedule Card”:

page 1007 “Job Planning Lines”:

In the current version, I have not found any examples of using objects other than codeunit and page in the standard. Since Microsoft mentioned all objects, let me test it in Table object briefly.
Test Version: W1 25.0 (Platform 25.0.23623.0 + Application 25.0.23364.23635)
Table: Can also be used.

PS:
1. When passing parameters there are two options; passing by value, which is the default behavior, or passing by reference, in which case you must specify the var keyword.

  • If a parameter is passed by value, then a copy of the variable is passed to the method. Any changes that the method makes to the value of the variable are local changes that affect only the copy, not the variable itself.
  • If a parameter is passed by reference, then a reference to the variable is passed to the method. The method can change the value of the variable itself.

this cannot be passed by reference.

2. Using this in Table Extension, Page Extension, or Report Extension can use the methods of source objects, just like a variable.
pageextension:

tableextension:

Great, give it a try!!!😁

PS:
1. Business Central 2024 wave 2 (BC25): Use the ternary operator ?: when coding in the AL language

2. Business Central 2024 wave 2 (BC25): new data type PageStyle

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL