Freigeben über


Walkthrough: Using Codeunits

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: a Set function and a Get function. The Set function sets an internal variable to the value of the parameter given. The Get function returns the value of the internal variable.

  • Creating one codeunit that has two codeunit variables that each call a function in the first codeunit and then assigns the codeunit variables 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 2018 installed with a developer license.

  • The CRONUS International Ltd. demo data company.

Story

Viktor is a Microsoft Certified Partner working for CRONUS International Ltd.. To learn about codeunits in Dynamics NAV, he plans to create a codeunit that includes Set and Get functions, create a codeunit that calls two instances of the Set_Get codeunit, and finally convert the codeunit to a single instance codeunit.

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

  1. Open Microsoft Dynamics NAV Development Environment.

  2. In the development environment, on the Tools menu, choose Object Designer.

  3. In Object Designer, choose Codeunit and then choose New.

  4. On the View menu, choose C/AL Globals.

  5. 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.

  6. In the C/AL GLobals window, choose the Functions tab.

  7. On the Functions tab, in the Name field, add two functions called Set and Get.

  8. Select the Set function, in the View menu, choose Properties, and then set the Local property to No.

    Setting this property makes the function accessible from the other codeunit that you will create. For more information about this property, see Local Property.

  9. Select the Set function and then choose Locals.

  10. 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.

  11. Close the Set -C/AL Locals window.

  12. In the C/AL GLobals window, select the Get function, and then choose Locals.

  13. In the Get - C/AL Locals window, choose the Return Value tab.

  14. In the Name field, enter output. In the Return Type field, select Integer from the drop-down list.

  15. Close the Get -C/AL Locals window.

  16. In the C/AL Editor for the codeunit, in the Set function section, enter the following 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.

  17. In the C/AL Editor, in the Get function section, enter the following code.

    output := InternalInt;  
    

    The code in the Get function specifies that the new value of the internal variable is the output value generated by this codeunit.

  18. Close the C/AL Editor. Choose Yes to save your changes to the codeunit.

  19. In the Save As window, in the ID field, enter 70000.

  20. In the Name field, enter Set_Get, select the Compile check box, and then choose the OK button.

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

  1. In Object Designer, choose Codeunit, and then choose New.

  2. On the View menu, choose C/AL Globals.

  3. On the Variables tab, in the Name field, enter CUInstance1 to create a new variable.

  4. In the DataType field, select Codeunit from the drop-down list. In the Subtype field, select codeunit 70000, Set_Get.

  5. In the C/AL GLobals window, on the Variables tab, in the Name field, enter CUInstance2 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.

  6. Close the C/AL GLobals window.

  7. In the C/AL Editor, add the following code to the OnRun trigger.

    CUInstance1.Set(1);  
    CUInstance2.Set(2);  
    MESSAGE('When running two instances, the value of CUInstance1 is %1 and the value of CUInstance2 is %2.', CUInstance1.Get, CUInstance2.Get);  
    
    CUInstance2 := CUInstance1;  
    MESSAGE('After assigning CUInstance2 to CUInstance1, the value of CUInstance1 is %1 and the value of CUInstance2 is also %2.', CUInstance1.Get, CUInstance2.Get);  
    

    The first two statements in this codeunit call two instances of the Set_Get codeunit and supply the input values that these instances should use. Next, the code prints a message with the return values of the two instances of the codeunit.

    CUInstance1 returns 2.

    CUInstance2 returns 3.

    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.

  8. Close the C/AL Editor and choose Yes to save your changes to the codeunit.

  9. In the Save As window, in the ID field, enter 70001. In the Name field, enter Assignment.

  10. Select the Compile check box, and then choose OK.

Testing the Codeunit

You will now test the codeunit that calls two instances of the Set_Get codeunit.

To test the codeunit

  1. In Object Designer, select codeunit 70001, and then choose Run. The following message is displayed: “When running two instances, the value of CUInstance1 is 2 and the value of CUInstance2 is 3.”

  2. On the message box choose OK. A second message box is displayed with the following message: "After assigning CUInstance2 to CUInstance1, the value of CUInstance1 is 2 and the value of CUInstance2 is also 2."

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

  1. In Object Designer, choose Codeunit, select codeunit 70000, and then choose Design.

  2. On the View menu, choose Properties.

  3. In the Set_Get - Properties window, in the SingleInstance field, select Yes from the drop-down list.

  4. Close the Set_Get - Properties window.

  5. On the File menu, choose Save As.

  6. In the Save As window, in the ID field, enter 70002.

  7. In the Name field, enter SingleInst and select the Compile check box, and then choose OK.

  8. In Object Designer, choose Codeunit, select codeunit 70000, and then choose Design.

  9. On the View menu, select C/AL Globals.

  10. On the Variables tab, in the CUInstance1 row, in the Subtype field, select codeunit 70002, SingleInst from the code list and then choose the OK button.

  11. On the Variables tab, in the CUInstance2 row, in the Subtype field, select codeunit 70002, SingleInst from the code list. Choose the OK button.

  12. Close the C/AL Globals window.

  13. In the C/AL Editor, replace the code in the OnRun trigger with the following code:

    CUInstance1.Set(7);  
    // A codeunit instance is created if one did not exist.  
    CUInstance2.Get();  
    // Returns 8, that is 7 + the 1 added by the Set function in   
    // codeunit 70002. CUInstance2 uses the same instance as CUInstance1,   
    // they use the same internal variables.  
    MESSAGE('The value of CUInstance2 is %1.', CUInstance2.Get);  
    
    

    CUInstance2.Get returns 8, which is the input value 7 + the 1 added by the Set function in codeunit 70002. CUInstance2 uses the same instance as CUInstance1, therefore, they use the same internal variables.

  14. Close the C/AL Editor.

  15. In the Save Changes window, select the Compiled check box, and then choose the Yes button.

Testing the Single Instance Codeunit

The single instance codeunit variables use the same set of variables.

To test the single instance code unit

  1. In Object Designer, select codeunit 70001, and choose Run. The following message is displayed, "The value of CUInstance2 is 8."

    The CUInstance2 instance of codeunit 70002 is able to access the value of the InternalInt variable that was set by the CUInstance1 instance of codeunit 70002. This is because codeunit 70002 is specified as a single instance codeunit.

    Note

    It is possible to use a single instance codeunit across objects, not just within the same object.

  2. In Object Designer, select codeunit 70002, and then choose the Design button.

  3. On the View menu, choose Properties.

  4. In the SingleInst - Properties window, in the SingleInstance field, select No from the drop-down list.

  5. Close the C/AL Editor.

  6. In the Save Changes window, select the Compiled check box, and choose Yes.

  7. In Object Designer, select codeunit 70001, and then choose Run. The following message is displayed, "The value of CUInstance2 is 0."

    Since codeunit 70002 is no longer a single instance codeunit, the CUInstance2 instance of codeunit 70002 is not able to access the value of the InternalInt variable that was set by the CUInstance1 instance of codeunit 70002.

See Also

Using Codeunits