Setting filters :)

Which code do you prefer? Integer and Field is of Integer type

Of course the second line of code is better!

Either could be better depending on context. I also expect a better code than one where a variable is a key word, so I dont vote on those two. Robert

This poll is more about which code do you use in your life [:)]

It depends of the situation. Sometimes you cannot use SETRANGE and have to do it with SETFILTER. But i prefer SETRANGE.

I don’t like SETRANGE in this situation just for one reason - filter on this Field looks like this: Integer..Integer But with SETFILTER it looks normal and is easier to understand for developers and users. I had some trouble with it before so that’s why I made poll [:)] That’s what I think.

In this case i woud prefer the second code because your going on an integer :smiley: IF it was a key-word (or something similar) i would use the first one. Usually i prefer SETFILTER but not on Numbers!

Two Different Uses If the field is part of the Key set by Record.RESET or Record.SETCURRENTKEY use SETRANGE else use SETFILTER If the field is in the key use Record.SETRANGE(Field,VALUE); If the field is not in the key use Record.SETFILTER(Amount,’<>%1’,Value); If the field is in the Key but you wish to select a range and exclude the current record by filter use both take a Document Line Record.SETRANGE(“Document Type”,“Document Type”); Record.SETRANGE(“Document No.”,“Document No.”); Record.SETFILTER(“Line No.”,’<>%1’,“Line No.”); The above code excludes the saved current record from the filtered set which you canot do with SETRANGE. You would maybe use this to get a total Amount if the Amount in rec is different to xrec Record.CALCSUMS(Amount); // Exit with the new Total Amount EXIT(Record.Amount + Rec.Amount);

It doesn’t matter if it’s part of the key or not for using SETRANGE or SETFILTER… The most easy way is: If the filter is just a value (example all records where code = ‘blue’) then use setrange. SETRANGE(Code,‘BLUE’); If the filter is a simple interval (example blue…red ), then you can also use setrange. SETRANGE(Code,‘BLUE’,‘RED’); If the filter is a negation or a more complex filter (AND,OR…) use SETFILTER; SETFILTER(Code,’>%1|%2’,‘BLUE’,‘RED’); SETFILTER(Code,’<>%1’,‘RED’); Regards,

I prefer to use SETRANGE whenever possible. I think in a training i heared that SETRANGE would be faster than SETFILTER. But - speed is often not the only citeria for coding it this way. If i’m ill or absent a colleague of me has to maintain my code. So it’s necessary to write code so anybody else can understand asap what i wanted to do. Hence i like to create s unit in a way it is readable even month or years later… [;)]

Internally Navision does not have a “SETRANGE” function, in fact at compile time the SETRANGE function is compiled as a SETFILTER command. So in reality it makes no difference. My personal preference is that SETRANGE is easier to read, so I use it where ever possible.

Suppose you have a form with a filter to select a country code. Let’s call the variable FormCountryFilter. When you type for example: Customer.SETRANGE(Country,FormCountryFilter); Then the filter will be reset if the user leaves the FormCountryFilter blank. So, here you will get all customers. When you type: Customer.SETFILTER(Country,’=%1’,FormCountryFilter); Then the filter will be set to equal to ‘’ if the user leaves the FormCountryFilter blank. So, here you will get all Customer records with a blank Country field. Conclusion: There can be a significant difference in the usage of SETRANGE and SETFILTER

But if you type: Customer.SETFILTER(Country,FormCountryFilter); Then it will be the same as SETRANGE :slight_smile: This also will work same way:Customer.SETFILTER(Country,'%1',FormCountryFilter);

the second one. also in my opinion it takes more time to read a setfilter sentence than a setrange sentence… but this is just another opinion. i choose setrange.

both lines makes same effect the second one is shortest that all!!


Originally posted by sebastien.bas-guasch
both lines makes same effect the second one is shortest that all!!

Nice lets see your SETRANGE for Countries AT & BE & CZ & DE Range of Records SETRANGE(“Country Code”,AT,DE); (AT…DE) Filtered Selection of Records SETFILTER(“Country Code”,’%1’,‘AT|BE|CZ|DE’);

Hi Dave, I have to disagree, the two statements are very different, especially from a performance point of view. The server search mechanism in your first scenario is setting a start and end point, i.e. ONE filter set, the second is setting FOUR filters, which must be slower. By the way maybe you meant SETFILTER("Country Code",'%1..%2','AT','DE'); which would then be the same.

I think both commands will be performed the same because SETRANGE with only left range will set the same filter as SETFILTER. Please object with detailed technical substantiation otherwise it’s too philosophical.

I actually need a white board to show this… hmmm. Basically by setting four individual filters, the search mechanism is obviously slower. There are two issues, one that the functionality of the two commands is identical, but the performance is OBVIOUSLY slower setting four seperate filters.

Just as a foot note, … in the old version of Navision, setfilter was actually faster than setrange. This was because the code was interpreted at run time, and obviously took longer to convert the command. But that is in the past, now that Navision compiles the code, both the comands listed by Arthur should be the same speed.