How To assign instance of Codeunit Variable.

lakshmanlakshman Member Posts: 44
Hi All,

can We assign one codeunit variable the instance of another codeunit variable.

please Suggest me.
Best Regards,
Lakshman Routu
http://lakshman-routu.blogspot.in/

Comments

  • BeliasBelias Member Posts: 2,998
    you mean:
    mycu1 ---> codeunit 50000
    mycu2 ---> codeunit 50000
    mycu1 := mycu2;
    

    it doesn't give you compile error, but what's the point in doing this? :-k
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • WaldoWaldo Member Posts: 3,412
    Is transfers the values of the global variables of the codeunit. That could be the reason, no :-k ?

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • BeliasBelias Member Posts: 2,998
    Waldo wrote:
    Is transfers the values of the global variables of the codeunit. That could be the reason, no :-k ?

    :? it is what i thought...but maybe i've done something wrong because it doesn't transfer global variables...
    i've done this:
    cu.FNTGetParTest;
    cu := cu2;
    cu2.FNTGetParTest;
    

    and in cu and cu2 .fntgetpartest there is:
    Par1 := Par1 + 100;
    Par2 := Par2 + 200;
    locpar := locpar + 555;
    

    debugging it, when cu2.fntgetpartest is called, par1 and par2 have 0 value...am i missing something? :-k
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • WaldoWaldo Member Posts: 3,412
    Yes, I think so.

    try switching cu and cu2.
    cu.FNTGetParTest; 
    cu2 := cu; 
    cu2.FNTGetParTest;
    

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • BeliasBelias Member Posts: 2,998
    no comment... :oops:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • WaldoWaldo Member Posts: 3,412
    :mrgreen:
    paska juttu

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • BeliasBelias Member Posts: 2,998
    Waldo wrote:
    :mrgreen:
    paska juttu
    what?
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • WaldoWaldo Member Posts: 3,412
    Shit happens...

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • lakshmanlakshman Member Posts: 44
    Hi All,

    Thanks for your Reply.


    I read one pdf file like below

    Codeunit Assignment:

    Codeunits can be treated as objects − one codeunit variable can be assigned to another codeunit variable, which creates a new reference to the same codeunit instance. In other words, the codeunit variables then use the same set of internal variables

    But i dont know how to assiagn one codeunit variable to another coudeunit variable thats why i posted it.
    Best Regards,
    Lakshman Routu
    http://lakshman-routu.blogspot.in/
  • WaldoWaldo Member Posts: 3,412
    Like belias said:
    CU1 := CU2;
    

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • BeliasBelias Member Posts: 2,998
    cu2 := cu;
    
    this one!
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • WaldoWaldo Member Posts: 3,412
    And it's important how it's stated.
    It's not how we thought: copying over the values of the variables. It's literally how it's stated: "...which creates a new reference to the same codeunit instance. In other words, the codeunit variables then use the same set of internal variables ".

    After assignment,
    if changing a variable in CU1, it'll be changed in CU2 as well. It is a reference to the same thing.
    So now I wouldn't know anymore why this would be useful.. :-k

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • krikikriki Member, Moderator Posts: 9,110
    Waldo wrote:
    So now I wouldn't know anymore why this would be useful.. :-k
    Me too. In my NAV-life, I have written a lot of (kriki-)code but I don't remember to have used this possibility once. :-k
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ara3nara3n Member Posts: 9,256
    kriki wrote:
    Waldo wrote:
    So now I wouldn't know anymore why this would be useful.. :-k
    Me too. In my NAV-life, I have written a lot of (kriki-)code but I don't remember to have used this possibility once. :-k

    This is very useful when you are posting and need to pass CU 22 or CU 12 to your own functions. These codeunits keep track of variables e.g. LastEntryNo. etc.

    So if you are inserting additional Item journal line entries or GL Entries, you can use the variable that is used in CU 80 or 90.
    Btw I've used this once and worked out well.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
Sign In or Register to comment.