This discussion has been locked.
You can no longer post new replies to this discussion. Posts are automatically locked, when no new replies have been made for a long time. If you have a question you can start a new discussion.

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 :D 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.
Related
Recommended