Is it need to CLEAR(codeunit)?

AndwianAndwian Member Posts: 627
Dear all,

I found this code in codeunit 5765 Whse.-Post Shipment + Print:
WITH WhseShipLine DO BEGIN
  IF FIND THEN BEGIN
    Selection := STRMENU(Text000,2);
    IF Selection = 0 THEN
      EXIT;
    Ship := Selection IN [1,2];
    Invoice := Selection IN [2];
  END;

  WhsePostShipment.SetPostingSettings(Invoice);
  WhsePostShipment.SetPrint(TRUE);
  WhsePostShipment.RUN(WhseShipLine);
  WhsePostShipment.GetResultMessage;
  CLEAR(WhsePostShipment); // What is it for?
END;

where there is a line with:
CLEAR(WhsePostShipment); // What is it for?

What is it for? Is it needed in every last call after the codeunit not used anymore?
Since I read in other places where this CLEAR statement did not explicitly typed.

Thank you in advance!
Regards,
Andwian

Comments

  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    Sometimes you find this statement at the beginning of code snippets, sometimes at the end and there are some special situations where it is set in between. It's to be sure that all global values within the codeunit are reset when the codeunit is called next time.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • RnRRnR Member Posts: 21
    Relevant only for singleinstance codeunits?
  • crisnicolascrisnicolas Member Posts: 177
    No, relevant to all kind of codeunits
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    I think CLEAR doesn't work for Single Instance codeunits. Even CLEARALL within Single Instance codeunit doesn't work as you can see in Online Help of CLEAR:
    Use the CLEARALL Function (VARIABLE) to clear all internal variables, keys, and filters in the object and in any associated objects such as reports, forms, and codeunits that contain C/AL code. Note, however, that CLEARALL does not affect or change values for variables in single instance codeunits.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • vaprogvaprog Member Posts: 1,139
    It's to be sure that all global values within the codeunit are reset when the codeunit is called next time.

    That's what CLEARALL is used for.
    CLEARALL (Variable)
    Use this function to clear all internal variables (except REC variables), keys, and filters in the object and in any associated objects, such as reports, forms, codeunits, and so on that contain C/AL code. CLEARALL works by calling CLEAR repeatedly on each variable. However, this is not the case with codeunits, where the CLEARALL function works by calling CLEARALL inside the codeunit. It deletes the contents of the codeunit, whereas CLEAR only deletes the reference to the codeunit.
    I must admit, I don't fully understand the implication of this, though, unless you created a copy of the codunit variable before and reuse either of these afterwards.
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    Of course I know that internal technique of these two functions is different. But purpose of CLEAR(Codeunit) is to be sure that you don't use some old values if you call that codeunit again. Reference will be cleared, hence a normal codeunit shouldn't have any reference on it, hence (if there is some kind of garbage collector) instance could be cleared as well.

    And if your are calling more than one function of the codeunit depending on some data or process then you won't know when to call CLEARALL within the codeunit.

    This might also be important:
    except REC variables
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • AndwianAndwian Member Posts: 627
    Thank you very much for all responses. :D
    RnR wrote:
    Relevant only for singleinstance codeunits?
    What is single instance codeunit?
    No, relevant to all kind of codeunits
    How many kind of codeunits are there? :-k
    Regards,
    Andwian
  • crisnicolascrisnicolas Member Posts: 177
    Andwian wrote:
    How many kind of codeunits are there? :-k

    Just single instance and non single instance (which is the most common)
    SingleInstance
    When you set this property to Yes on codeunit X, all codeunit variables that use codeunit X will use the same instance. That is, all codeunit variables of codeunit X will use the same set of internal variables when the code is running on the same client. Codeunit X remains instantiated until you close the company.
  • AndwianAndwian Member Posts: 627
    Navision Help wrote:
    SingleInstance
    When you set this property to Yes on codeunit X, all codeunit variables that use codeunit X will use the same instance. That is, all codeunit variables of codeunit X will use the same set of internal variables when the code is running on the same client. Codeunit X remains instantiated until you close the company.
    I am sorry, I still do not understand with the Single Instance Codeunit.
    Regards,
    Andwian
  • kitikkitik Member Posts: 230
    Try this example and will see the diference
    1. Create a codeunit and a report as the ones in the example
    2. Run the report
    3. Change the codeunit to SingleInstance
    4. Run the report
    5. Run the report again
    6. Open a new database session (now you have two clients open)
    7. Run the report again with the new session

    Salut!
    Laura Nicolàs

    CODEUNIT
    OBJECT Codeunit 50000 MyCodeunit
    {
      OBJECT-PROPERTIES
      {
        Date=25/08/10;
        Time=14:17:11;
        Modified=Yes;
        Version List=;
      }
      PROPERTIES
      {
        OnRun=BEGIN
                IntValue := IntValue + 1;
              END;
    
      }
      CODE
      {
        VAR
          IntValue@1000000000 : Integer;
    
        PROCEDURE GetIntValue@1000000000() : Integer;
        BEGIN
          EXIT(IntValue);
        END;
    
        BEGIN
        END.
      }
    }
    

    REPORT
    OBJECT Report 50000 MyReport
    {
      OBJECT-PROPERTIES
      {
        Date=25/08/10;
        Time=14:24:29;
        Modified=Yes;
        Version List=;
      }
      PROPERTIES
      {
      }
      DATAITEMS
      {
        { PROPERTIES
          {
            DataItemTable=Table2000000026;
            DataItemTableView=SORTING(Number);
            MaxIteration=10;
            OnAfterGetRecord=BEGIN
                               MyCU1.RUN;
                               Value1 := MyCU1.GetIntValue;
    
                               MyCU2.RUN;
                               Value2 := MyCU2.GetIntValue;
                             END;
    
          }
          SECTIONS
          {
            { PROPERTIES
              {
                SectionType=Header;
                SectionWidth=12000;
                SectionHeight=423;
              }
              CONTROLS
              {
                { 1000000002;Label  ;0    ;0    ;1500 ;423  ;FontBold=Yes;
                                                             CaptionML=ENU=MyCU1 var }
                { 1000000003;Label  ;1800 ;0    ;1500 ;423  ;FontBold=Yes;
                                                             CaptionML=ENU=MyCU2 var }
              }
               }
            { PROPERTIES
              {
                SectionType=Body;
                SectionWidth=12000;
                SectionHeight=423;
              }
              CONTROLS
              {
                { 1000000000;TextBox;0    ;0    ;1500 ;423  ;SourceExpr=Value1 }
                { 1000000001;TextBox;1800 ;0    ;1500 ;423  ;SourceExpr=Value2 }
              }
               }
          }
           }
      }
      REQUESTFORM
      {
        PROPERTIES
        {
          Width=9020;
          Height=3410;
        }
        CONTROLS
        {
        }
      }
      CODE
      {
        VAR
          MyCU1@1000000000 : Codeunit 50000;
          MyCU2@1000000001 : Codeunit 50000;
          Value1@1000000002 : Integer;
          Value2@1000000003 : Integer;
    
        BEGIN
        END.
      }
    }
    
    Laura Nicolàs
    Author of the book Implementing Dynamics NAV 2013
    Cursos Dynamics NAV (spanish) : http://clipdynamics.com/ - A new lesson released every day.
  • AndwianAndwian Member Posts: 627
    Thank you Laura. I will try it.
    Regards,
    Andwian
Sign In or Register to comment.