You can treat codeunits as objects. One codeunit variable can be assigned to another codeunit variable, which creates a new reference to the same codeunit instance. The codeunit variables then use the same set of internal variables.
About This Walkthrough
This walkthough illustrates the following tasks:
-
Creating one codeunit that has two functions: Set and Get. Set sets an internal variable to the value of the parameter given. Get returns the value of the internal variable.
-
Creating one codeunit that has two variables that call the first codeunit and are then assigned to each other so that they use the same instance of the first codeunit.
-
Modifying one of these codeunits to make it a single instance codeunit.
Prerequisites
To complete this walkthrough, you will need:
-
Microsoft Dynamics NAV installed with a developer license
-
The CRONUS International Ltd. demo data company installed
Story
Viktor is a Microsoft Certified Partner working for CRONUS International. To learn about codeunits in Microsoft Dynamics NAV, he plans to create some codeunits and use variables in those codeunits in different ways.
Creating the First Codeunit
First, you must create a codeunit that includes the Set and Get functions. You must also write the code for these two functions.
To create the first codeunit
-
In the Classic client, click the Tools menu, and then click Object Designer.
-
In Object Designer, click Codeunit and then click New.
-
In the View menu, click C/AL Globals.
-
On the Variables tab, in the Name field, enter InternalInt to create a new variable. In the DataType field, select Integer from the drop-down list.
-
In the C/AL GLobals window, click the Functions tab.
-
On the Functions tab, in the Name field, add two functions called Set and Get.
-
Select the Set function and then click Locals.
-
In the Set - C/AL Locals window, on the Parameters tab, in the Name field, enter input. In the DataType field, select Integer from the drop-down list.
-
Close the Set - C/AL Locals window.
-
In the C/AL GLobals window, select the Get function, and then click Locals.
-
In the Get - C/AL Locals window, click the Return Value tab.
-
In the Name field, enter out. In the Return Type field, select Integer from the drop-down list.
-
Close the Get - C/AL Locals window.
-
In the C/AL Editor for the codeunit, in the Set function section, enter the following code.
Copy Code InternalInt := input + 1; Get;
The code in the Set function increases the value of the input by one and sets this as the value of the internal variable. It then calls the Get function.
-
In the C/AL Editor, in the Get function section, enter the following code.
Copy Code out := InternalInt;
The code in the Get function specifies that the new value of the internal variable is the output value generated by this codeunit.
-
Close the C/AL Editor. Click Yes to save your changes to the codeunit.
-
In the Save As window, in the ID field, enter 70000. In the Name field, enter Set_Get. Select the Compile check box, and then click OK.
Creating the Second Codeunit
The next step is to create the codeunit that calls two instances of the Set_Get codeunit and supplies the input values that these instances should use. This codeunit contains two variables, which are assigned to each other so that they both use the same instance of codeunit 70000.
To create the second codeunit
-
In Object Designer, click Codeunit, and then click New.
-
In the View menu, select C/AL Globals.
-
On the Variables tab, in the Name field, enter CoMIC1 to create a new variable. In the DataType field, select Codeunit from the drop-down list. In the Subtype field, select codeunit 70000, Set_Get.
-
In the C/AL GLobals window, on the Variables tab, in the Name field, enter CoMIC2 to create a second variable. In the DataType field, select Codeunit from the drop-down list. In the Subtype field, select codeunit 70000, Set_Get.
-
Close the C/AL GLobals window.
-
In the C/AL Editor, add the following code to the OnRun trigger.
Copy Code CoMIC1.Set(1); CoMIC2.Set(2); // Calls two instances of codeunit 70000 // with the input parameters 1 and 2, respectively. MESSAGE('When running two instances, the value of CoMIC1 is %1 and the value of CoMIC2 is %2.',CoMIC1.Get,CoMIC2.Get); // The message retrieves the return values from codeunit 70000 // and shows them in the message. // CoMIC2 returns 2. // ComIC2 returns 3, because you have added 1 to each parameter. CoMIC2 := CoMIC1; // CoMIC2 is assigned to CoMIC1 // and they both use the same instance of codeunit 70000. MESSAGE('After assinging CoMIC2 to CoMIC1, the value of CoMIC1 is %1 and the value of CoMIC2 is also %2.',CoMIC1.Get,CoMIC2.Get); // CoMIC1 and CoMIC2 now show the same value.
The first statement in this codeunit calls two instances of the Set_Get codeunit and supplies the input values that these instances should use. The next statement assigns the two variables to each other so that they both use the same instance of codeunit 70000 and therefore generate the same return value.
-
Close the C/AL Editor. Click Yes to save your changes to the codeunit.
-
In the Save As window, in the ID field, enter 70001. In the Name field, enter Assignment. Select the Compile check box, and then click OK.
Using a Single Instance Codeunit
In some cases, only one instance of a codeunit needs to exist. This means that all the codeunit variables of a particular codeunit use the same set of variables. You can make a codeunit a single instance codeunit by setting the SingleInstance property to Yes.
To modify a codeunit to a single instance codeunit
-
In Object Designer, click Codeunit, select codeunit 70000, and then click Design.
-
In the View menu, click Properties.
-
In the Properties window, in the SingleInstance field, select Yes from the drop-down list.
-
Close the Set_Get - Properties window.
-
In the File menu, click Save As.
-
In the Save As window, in the ID field, enter 70002. In the Name field, enter SingleInst. Select the Compile check box, and then click OK.
-
In Object Designer, click Codeunit, select codeunit 70000, and then click Design.
-
In the View menu, select C/AL Globals.
-
On the Variables tab, in the Name field, enter CoMIC1. In the DataType field, select Codeunit from the drop-down list. In the Subtype field, select codeunit 70002, SingleInst.
-
On the Variables tab, in the Name field, enter CoMIC2. In the DataType field, select Codeunit from the drop-down list. In the Subtype field, select codeunit 70002, SingleInst.
-
Close the C/AL Globals window.
-
In the C/AL Editor, add the following code to codeunit 70000 to the OnRun trigger:
Copy Code CoMIC1.Set(7); // A codeunit instance is created if one did not exist. CoMIC2.Get(); // Returns 8, that is 7 + the 1 added by the set function in // codeunit 70002. CoMIC2 uses the same instance as CoMIC1, // they use the same internal variables. MESSAGE('the value of CoMIC2 is %1',CoMIC2.Get);
-
Close the C/AL Editor. In the Save Changes window, select the Compiled check box, and then click Yes to save your changes.
-
In Object Designer, select codeunit 70000, and then click Run. The following message appears, "The value of CoMIC2 is 8"
The input value in codeunit 70000 is 7 and codeunit 70002 adds 1 to this value.
-
In Object Designer, select codeunit 70002, and then click Design.
-
In the Set function, change the first line to the following.
InternalInt := input+4
. -
Close the C/AL Editor. In the Save Changes, window, select the Compiled check box, and then click Yes to save your changes.
-
In Object Designer, select codeunit 70000, and then click Run. You will get the same message that you received in step 14. This occurs because after you have loaded a single instance codeunit, it remains loaded on the client until you restart the client.
Note It is possible to use a single instance codeunit across objects, not just within the same object.
See Also
© 2010 Microsoft Corporation. All rights reserved.