Options

NEXT function with FIND('+')

blhblh Member Posts: 24
I’m looking at a bit of code put in a codeunit by my predecessor and I’m a little confused. Here’s what the code that has me a bit perplexed:
IF Rate.Tbl FIND(‘+’) THEN
REPEAT
Logic which assigns values to some variables
UNTIL RateTbl.NEXT=0;
I understand that FIND(‘+’) finds the last record in a set and that the variables will be assigned values until there are no more records in the set. Here’s where I get a little lost. In all the documentation I’ve read on using FIND in conjunction with NEXT the value 0 always moves one record forward. If the code starts with the last record in the set and moves forward then it will only read one record which appears to make the REPEAT…UNTIL section of the code unnecessary. If in fact NEXT=0 can also move backwards through a set of records then I can understand what’s happening. If that were the case the logic would repeat starting with the last record until it finds the first. So my question is this: Can the NEXT=0 actually be reading backwards in the set or is there some other possible reason why a programmer would have used a REPEAT…UNTIL section of code that apparently won’t repeat? Any help would be greatly appreciated. Thanks!

Comments

  • Options
    sergisoftsergisoft Member Posts: 37
    Are you sure this is the line: "UNTIL RateTbl.NEXT=0;" ?
    I think it's more logical to put "UNTIL RateTbl.NEXT(-1)=0;" which means go to back record.

    Another solution is to put a message inside the loop to test if it appears more than a once.

    Sergi Navarro

    MCTS: Microsoft Dynamics™ NAV 2009 C/SIDE Solution Development

    Visit my Navision Spanish Blog here: http://www.sergisoft.com
  • Options
    kinekine Member Posts: 12,562
    May be that the code inside the loop is changing the position in the set somehow. But the code as was posted is strange... 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    SpawnSpawn Member Posts: 10
    It is true that FIND(‘+’) is finding the last record in the set. When you are executing NEXT operator – system try to get next record (going forward) which is not exist, because you are already at the end of the set.

    You need to go backward in the recordset to process all the records. You can, as sergisoft already mentioned, instead of NEXT, which is actually NEXT(1), use NEXT(-1). It means that you will go one record backward every time in the loop.

    So NOT
    IF Rate.Tbl FIND(‘+’) THEN
    REPEAT
    Logic which assigns values to some variables
    UNTIL RateTbl.NEXT = 0;

    But

    IF Rate.Tbl FIND(‘+’) THEN
    REPEAT
    Logic which assigns values to some variables
    UNTIL RateTbl.NEXT(-1) = 0;

    It must solve your problem.
  • Options
    SpawnSpawn Member Posts: 10
    As a solution you can also change the sort order in the record set from ascending to descending and use standard loop processing.

    Rec.ASCENDING(FALSE);
    IF Rec.FIND('-') THEN
    REPEAT
    MESSAGE(FORMAT(Rec));
    UNTIL Rec.NEXT = 0;
  • Options
    BeckaBecka Member Posts: 178
    FINDFIRST = FIND('-')
    FINDLAST = FIND('+')
    MCSD
    Attain Navision
  • Options
    SpawnSpawn Member Posts: 10
    2Becka.

    Usage of FIND(‘-’) or FINDFIRST very depends from the version of NAV you are using.
    And in any case with REPEAT-UNTIL statement - best practice is to use FINDSET, not FINDFIRST :)
  • Options
    DenSterDenSter Member Posts: 8,304
    Becka wrote:
    FINDFIRST = FIND('-')
    FINDLAST = FIND('+')
    No that is not true. FIND('-') and FIND('+') get the entire set of records, FINDFIRST and FINDLAST only gets the first or the last record, one record only. You cannot simply replace all instances of FIND('-') and FIND('+') with FINDFIRST and FINDLAST, that's only going to make problems worse.

    In fact, most of the time FIND('-') is replaced by FINDSET, because most of the time the intention is to loop through records.
  • Options
    DenSterDenSter Member Posts: 8,304
    Spawn wrote:
    Usage of FIND(‘-’) or FINDFIRST very depends from the version of NAV you are using.
    I think this is a language problem, and it is lost in translation. Just to be clear, FINDFIRST is available from a certain version (4.0 SP1 IIRC), but once you are on a version that provides those keywords, the version itself has no part in deciding which one to use. That depends on the intention of the code, not the version.
  • Options
    idiotidiot Member Posts: 651
    Spawn wrote:
    Usage of FIND(‘-’) or FINDFIRST very depends from the version of NAV you are using.
    And in any case with REPEAT-UNTIL statement - best practice is to use FINDSET, not FINDFIRST :)

    Usage of FIND(‘-’) or FINDFIRST depends on, in order of relevance
    1. the programmer
    2. the version of Nav, SQL or Native
    3. the required return of results
    NAV - Norton Anti Virus

    ERP Consultant (not just Navision) & Navision challenger
  • Options
    DenSterDenSter Member Posts: 8,304
    idiot wrote:
    Usage of FIND(‘-’) or FINDFIRST depends on, in order of relevance
    1. the programmer
    No I don't agree, it's mostly the intention of the code that determines which one you use.

    The keywords FIND('-') and FINDFIRST are not interchangeable, they simply do not mean the same thing.
  • Options
    WaldoWaldo Member Posts: 3,412
    I agree with Denster.
    Some time ago, I wrote a series of blog articles, called "What impact does my C/AL have on SQL".

    Specifically for the find-statements, you can find more here.

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • Options
    idiotidiot Member Posts: 651
    DenSter wrote:
    No I don't agree, it's mostly the intention of the code that determines which one you use.

    The keywords FIND('-') and FINDFIRST are not interchangeable, they simply do not mean the same thing.

    I can show you a few consultants who does not even know FINDSET, FINDFIRST existed until version 5.0 came out.
    They had been working so hard at client's location that they were not aware of the new features.
    If you don't know about it how can you even think of using it?
    NAV - Norton Anti Virus

    ERP Consultant (not just Navision) & Navision challenger
  • Options
    DenSterDenSter Member Posts: 8,304
    You're putting the horse behind the wagon. Just because a few consultants don't know about some of the most important keywords does not mean that it's the programmer that determines the relevance of using any keyword.

    The fact remains that you are suggesting that FIND('-') and FINDFIRST are interchangeable, which clearly they are not.
  • Options
    idiotidiot Member Posts: 651
    One of us is definitely either having problem understanding English or taking things with a lot of assumptions in place.
    And I think I've laid down my criteria for my statements pretty obviously.
    My point is if a programmer does not know the existence of FINDSET, how does the programmer use it? Where does your "relevance" even comes in? The programmer must first be educated about the new FINDSET features & its capabilities so as to know when & how to use it. And I'm making reference to the same programmer, ceteris paribus.


    When have I ever gave the opinion that FIND('-') and FINDFIRST are interchangeable?
    NAV - Norton Anti Virus

    ERP Consultant (not just Navision) & Navision challenger
  • Options
    DenSterDenSter Member Posts: 8,304
    idiot wrote:
    One of us is definitely either having problem understanding English or taking things with a lot of assumptions in place.
    I have no problem with English, you?
    idiot wrote:
    When have I ever gave the opinion that FIND('-') and FINDFIRST are interchangeable?
    That's where:
    idiot wrote:
    Usage of FIND(‘-’) or FINDFIRST depends on, in order of relevance
    idiot wrote:
    My point is if a programmer does not know the existence of FINDSET, how does the programmer use it?
    My point is that I just don't agree that the programmer's skill level determines which keyword to use. Like I said before, that is putting the horse behind the cart.
  • Options
    idiotidiot Member Posts: 651
    idiot wrote:
    Usage of FIND(‘-’) or FINDFIRST depends on, in order of relevance
    1. the programmer
    2. the version of Nav, SQL or Native
    3. the required return of results
    DenSter wrote:
    ...determines the relevance of using any keyword.

    I'm not down to the "relevance of using any keyword" yet. I'm still at knowledge of programmer. You're too fast.
    If a programmer is unaware that FINDSET exists, he can only use FIND('-') do you agree?
    NAV - Norton Anti Virus

    ERP Consultant (not just Navision) & Navision challenger
  • Options
    SogSog Member Posts: 1,023
    Or to put it bluntly
    Idiot describes the usage of the FIND* functions as it is in real life (often as it not should be)
    while DenSter, Waldo, ... describe the usage of the FIND* functions how it should be.
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • Options
    WaldoWaldo Member Posts: 3,412
    I was starting to get it ;°)

    And I agree ... I still see quite many people wrongly use these statements... even don't care about these statements. Just got someone on a job interview. 6 years experience in developing C/AL. But he wasn't able to use the FIND* statements in a decent way ...

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • Options
    DenSterDenSter Member Posts: 8,304
    idiot wrote:
    I'm not down to the "relevance of using any keyword" yet. I'm still at knowledge of programmer. You're too fast.
    I don't get that, YOU put the programmer as number 1 in YOUR list or relevance, how can I be too fast for your first point? I get your point, I understand what you are trying to say, I simply don't agree with it. The programmer's skill simply is no factor in deciding which keyword to use. You don't say "a junior uses FIND('-') but a senior should use the more advanced FINDFIRST" that just doesn't make any sense at all. Programmers at any skill level should always use the right keyword, depending on the requirement. It's the functional requirement and the technical design that determines what keywords to use, not the programmer's skill level.
    idiot wrote:
    If a programmer is unaware that FINDSET exists, he can only use FIND('-') do you agree?
    You are right, this IS about language, because you're not making any sense to me. You started out determining how the relevant factors when deciding to use FIND('-') or FINDFIRST, now you mix in FINDSET, and you say that we're not even at the point where the programmer knows about FINDFIRST. If the programmer doesn't know about FINDFIRST, there is no decision of which one to use, so this whole question becomes irrelevant. If they only know about FIND('-') then there is no choice. No choice means there is no decision. No decision means that all factors are irrelevant.

    By the way, those keywords have been available for years now. Any NAV programmer that still doesn't know how to use them has some serious competency issues in my opinion.
  • Options
    idiotidiot Member Posts: 651
    Sog & Waldo knows what I'm implying & encountering.

    The reason for this
    DenSter wrote:
    ...because you're not making any sense to me...

    is because you do not believe there exist people who
    DenSter wrote:
    By the way, those keywords have been available for years now. Any NAV programmer that still doesn't know how to use them has some serious competency issues in my opinion.
    NAV - Norton Anti Virus

    ERP Consultant (not just Navision) & Navision challenger
  • Options
    DenSterDenSter Member Posts: 8,304
    I never said I don't believe there are people who don't know things, there are many people like that, in fact I am one myself. I just don't agree with you that the developer's skill level is a reason to choose a certain keyword. Any keyword.

    You don't choose keyword number 1 because someone is a rookie (or newbie, or greenhorn, or unexperienced, or stupid), you choose the keyword because the functionality calls for it. Not knowing a keyword is another discussion about competency, but for sure it is not the reason to use it.
Sign In or Register to comment.