Custom Functions with OPTIONAL parameters?

abartonicek
abartonicek Member Posts: 162
edited 2005-11-04 in Navision Attain
Can this be done?
Better to be critical then self-critical :)

Comments

  • Marije_Brummel
    Marije_Brummel Member, Moderators Design Patterns Posts: 4,262
  • abartonicek
    abartonicek Member Posts: 162
    :(

    Who would need that, right :)
    Better to be critical then self-critical :)
  • jhoek
    jhoek Member Posts: 216
    But there may be some work-arounds! :wink: Please explain what it is you're trying to achieve.
    Kind regards,

    Jan Hoek
    Product Developer
    Mprise Products B.V.
  • kriki
    kriki Member, Moderator Posts: 9,121
    Some trick. It is a trick to emulate optional parameters.

    1) Put your function that needs optional parameters in a codeunit.
    2) Create 1 or more extra functions that can be called BEFORE your function to pass the optional parameters, then call the your function

    eg. a function that adds lots of 2 or more decimals and gives the result
    CODEUNIT START
    Function Add(IdecDecimal1 AS Decimal;IdecDecimal2 AS Decimal): OdecReturnValue As Decimal
    BEGIN
      OdecReturnValue := IdecDecimal1 + IdecDecimal2;
      tmpSomeTempTable.RESET;
      IF tmpSomeTempTable.FIND('-') THEN
        REPEAT
          OdecReturnValue += tmpSomeTempTable."Optional Decimal";
          tmpSomeTempTable.DELETE(FALSE);
        UNTIL tmpSomeTempTable.NEXT = 0;
    END;
    
    Function AddOptionalDecimal(IdecOptionalDecimal As Decimal)
    BEGIN
      intUniqueEntry += 1;
      CLEAR(tmpSomeTempTable);
      tmpSomeTempTable."Entry No." := intUniqueEntry;
      tmpSomeTempTable."Optional Decimal" := IdecOptionalDecimal;
      tmpSomeTempTable.INSERT(FALSE);
    END;
    CODEUNIT STOP
    

    How to use it:
    cduMyCodeunit.AddOptionalDecimal(4);
    cduMyCodeunit.AddOptionalDecimal(8);
    MESSAGE('Total = %1',cduMyCodeunit.Add(1,2));
    

    Didn't test the code, so there can be some typo in it.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • awarn
    awarn Member Posts: 261
    Hows this - create a new table, each field in the table will be an optional parameter. The table does not need to be in the license range.

    The table record is the only parameter in the function.

    Fill the table record, DONT insert, but pass it into the function.

    Within the function now some of the 'fields' in the table will be filled and some will not.

    Short example:

    New table:

    Fields:
    1 - Item No.
    2 - Description
    3 - Price
    4 - Cost
    5 - Salesperson
    6 - Vendor
    7 - etc...


    Code to call function:

    NewTable.init; (to clear out values)
    NewTable.Item No. := '123456';
    NewTable.Cost := 99.00;
    NewTable.Vendor := =AAA';

    NewFunction(NewTable);


    The NewFunction looks like this:

    One parameter, type NewTable

    NewFunction(NewTable)

    If NewTable.Price = 0 then begin
    //conditional code here

    end else if NewTable.Cost > 10000 then begin
    //conditional code here

    End else if...




    -a
  • Marije_Brummel
    Marije_Brummel Member, Moderators Design Patterns Posts: 4,262
    Super TIP!

    And if you make the table TEMPORARY you don't need to have it in the customer license :D

    Or is this already handled by not inserting the record? :o
  • awarn
    awarn Member Posts: 261
    I'm not sure - but making temporary is probably the best idea - I always wait until I see the error [-o<

    -a