Dynamics 365 Business Central: “this” is a nice AL addition.

If someone is familiar with C#, probably knows that the this keyword is used to refer to the current instance of the class. It is used to access members from the constructors, instance methods, and instance accessors. this keyword is also used to track the instance which is invoked to perform some calculation or further processing related to that instance.

The AL language version 14 (Dynamics 365 Business Central 2024 Wave 2 release) introduces a new this keyword too for self-reference on all objects. The main benefits of this new additon 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.

The new this keyword works on every AL object but probably codeunits and interfaces is where you could use it a lot. Here some example of usage:

Then I can define a new codeunit with a procedure that accepts an input parameter with codeunit as type:

And when calling this Demo2Procedure method I can pass this as value (current instance of the Demo1 codeunit):

If you have an enum where “this” is one of the possible values:

when you reference that value from code, just remember to use the “this” syntax or you will have a compiler error:

Another useful usage of the this keyword is when using manual binding for event subscriptions. You can now use BindSubscription(this) and UnbindSubscription(this) as follows:

As said before, the new this keyword is for all AL objects. Here is an example of usage in a page object:

As you can see, in other objects where Curr* is used (CurrPage, CurrTable, CurrReport etc.) the new keyworkd seems ambiguous. The rule is to keep the Curr* keyword in use, but just remember that this is another option.

P.S. if you activate code analyzers, missing usage of this is signaled as a warning (AA0248). I personally don’t agree on this, probably things will change here.

The new this clause is not a revolutionary change, but I personally love this “object oriented” additions to the AL language too…

4 Comments

  1. In the example object codeunit 50114 “SD Manual Binding”, how is the state preserved so the PostingNoSeries is known in the procedure subscribing to the “Combine Shipments” report?

    Like

    1. It’s a manual codeunit, so you have to call the codeunit at another place. (e.g. OnBeforeAction or something)

      SDManualBinding.SetPostingNoSeries(‘NOSERIES’);
      SDManualBinding.ManualBinding();

      Like

  2. Could you please elaborate on the Enum example? That isn’t clear to me. Isn’t the ‘this’ option just a value as would be ‘test’?

    Like

    1. Yes in the enum it’s just a value like ‘test’. Here I want only to signal the usage. You can use MyEnum::test but if you use MyEnum::this compiler gives an error. You need to use MyEnum::”this”.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.