Sunday, December 18, 2016

Difference between RESET and CLEAR - Tips, Tricks & Facts #9

Hi guys,

As it is pretty clear from the heading, I am going to compare rec.RESET and CLEAR(rec) in this post.

We are aware of the fact that CLEAR function is used to clear variables of all types. I am going to stick to how does CLEAR work with record type variables for this post.


So what is the difference in using the 2 sections of code below,

Code Section 1:

rec.RESET;
rec.SETRANGE(...);
// Set some other filters then find records and use them as required

Code Section 2:

CLEAR(rec);
rec.SETRANGE(...);
// Set some other filters then find records and use them as required 

The differences between 1 and 2 is not much. Both code sections 1 & 2 will,
  • Remove all filters from the record variable.
  • Remove marks from marked records in the record variable.
  • Remove special filters like MARKEDONLY from the record variable.
  • Change the current key to Primary key of the record variable.

The one actual difference which I found out in my R&D is that (besides the points mentioned above) CLEAR also initializes the record variable, somewhat like INIT (not exactly like INIT). CLEAR sets the values of all the fields to the values mentioned in the InitValue Property of these fields.

Question - When is it beneficial using CLEAR instead of RESET in our code?

It totally depends upon the requirement on when may we use CLEAR and when to use RESET. Most of the times RESET will do just fine. CLEAR will rarely be required to be used with record variables.

I encountered such a rare situation some time back. I apologize that I cannot find that code, since I forgot where I did it (was a rare situation :)), if I find it I will share it with you guys in this post.

Meanwhile, please share your thoughts and comments below. Let me also know if there are any other differences that you know!

Thank you & Keep learning!