Get record and check field value in one "IF" statement

_prt_prt Member Posts: 2
Is it safe to get record and check it's field's value in one "IF" statement? For example:
IF UserSetup.GET(USERID) AND (UserSetup."Main Menu ID" <> 0) THEN BEGIN
  //do something
END;
looks like this is works fine, but
IF (UserSetup."Main Menu ID" <> 0) AND UserSetup.GET(USERID) THEN BEGIN
  //do something
END;

don't works and seems like Nav computes predicates from left to right

Comments

  • postsauravpostsaurav Member Posts: 708
    Hi,
    Yes it will match the conditions starting from left to Right. First you will get the record and once the record is there then you can check the condition.

    Thanks & Regards,
    Saurav Dhyani

    Do you Know this About NAV?


    Connect - Twitter | Facebook | Google + | YouTube

    Follow - Blog | Facebook Page | Google + Page
  • mdPartnerNLmdPartnerNL Member Posts: 802
    First example looks ok and logical to me. But never used it because i found situations in where it didn't work.

    Now all c/al code is converted to C# and it works as expected.
  • samlieshoutsamlieshout Member Posts: 7
    edited 2015-11-25
    While it evaluates from left to right, you should keep in mind it doesn't evaluate lazily.
    For example
    IF FuncA AND FuncB THEN ...
    
    will execute both functions even if FuncA returns false (e.g. a GET fails).

    If you want lazy evaluation you can use 'IN' e.g.
    IF NOT (FALSE IN [FuncA,FuncB]) THEN ...
    
    will perform a lazy AND operation.
    Thanks, Sam.
    Microsoft Dynamics NAV | App Service Engineer | Comments are my own
  • _prt_prt Member Posts: 2
    Thanks for your responses.
    But never used it because i found situations in where it didn't work.
    Can you tell more about these situations?
  • mdPartnerNLmdPartnerNL Member Posts: 802
    samlieshout example demonstrates a situation in where left to right doesn't work;
    IF FuncA AND FuncB THEN ...

    but am not sure if this will not work in 2013 and higher??
  • samlieshoutsamlieshout Member Posts: 7
    samlieshout example demonstrates a situation in where left to right doesn't work;
    IF FuncA AND FuncB THEN ...

    but am not sure if this will not work in 2013 and higher??

    Yeah, this behaviour is the same in the recent releases.
    Thanks, Sam.
    Microsoft Dynamics NAV | App Service Engineer | Comments are my own
  • mdPartnerNLmdPartnerNL Member Posts: 802
    would love to see the created C# code for this IF. As I understand, c# will run from left to right.
Sign In or Register to comment.