Using CLEAR for .NET Variables

ta5ta5 Posts: 1,144Member
We often see CLEAR commands for .NET Variables at the end of functions, for example in Codeunit 5054 "WordManagement".
The question: Why is it used? As far as I understand, local variables are cleared anyway when function ends and global functions are cleared as soon as the instance of the objects is not used anymore.
Btw: I know I seems to be good practice, but I wonder if it is really needed and what happens if not following this good practice (of course apart from the fact we'll get caught by the boegeyman) :#

Looking forward to your opinions.
Thanks, Thomas

Best Answer


  • Slawek_GuzekSlawek_Guzek Posts: 1,196Member

    I think it is uses as a part of good practice. For local variables it does not matter.

    For global is quite important imho, as you never know when your object cease to exists. If you don't clear it up yourself in your code you are basically counting on other developers that they will clear their vars. Which may or may not happen.

    I've seen many, many problems caused by using global vars and not clearing them afterwars. The scenario in most cases was - someone usd global var and at the time everyting was fine. Next time / next change the object with global var was itself declared as global var - also not cleared between call. Results - easy to guess.
    Slawek Guzek
    "For a truly gifted programmer, writing code is a side effect of thought." Paul Ford.
  • ta5ta5 Posts: 1,144Member
    Hi Slawek

    Thanks for your comment on this topic.

    Have a look at Codeunit 5054 "Word Management", Function "Execute Merge". This (local) Function uses CLEAR at the end. Would you basically say the CLEAR functions issued at the end do not matter?
    This is also my opinion and also what I've experienced, but on the other hand why is the code there if it is "for nothing"?

    Why am I asking this: I have and had discussions with fellow programmers and one reason to use CLEAR is always, because Microsoft does it in their own Codeunits.

  • ta5ta5 Posts: 1,144Member
    Hi Slawek
    Thx, fully agree.
Sign In or Register to comment.