How to create a function with a return value - OPTION?

easy-navieasy-navi Member Posts: 31
Hello,
Is there any possibility in C/AL to create a function with a return value of a type OPTION ???


Until now - on a form I had a function called ChceckProtocolExists with a return value - boolean (True, False).
It was easy to use - on a post button of that form I wrote:
IF NOT ChceckProtocolExist() THEN 
 ERROR('You cannot Post until there is no protocol');

But.. :( Once there appeared a documents that does NOT need a protolos AND should be POSTED.

So the most logical solution is to modify a function ChceckProtocolExists to return 3 values:
Yes, No, Not applicable.

And there is a problem, because I cannot force that function to return a variable of a Option Type :(
Any idea ??
http://www.reinwestuj.pl Inwestuj w nieruchomości. Condohotele, aparthotele.

Answers

  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    Use Integer as a return value: then you can return 0 or 1 or 2 or 3 ...
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • vaprogvaprog Member Posts: 1,141
    The designer does not allow you to use Option type as a return parameter. So, you have two possibilities
    • Create a procedure with no return value but a variable parameter instead, to which you assign the result
    • use an Integer as the return value. Option and Integer are interchangeable (though generally not good programming style). You may use a variable of type Option within your code to make it visually more clear what the code is doing.
    I'd strongly prefer the latter.

    The consumer of your function needs to either consume it as integer anyway, or define another variable with a similarly looking option list. Since there is no Option type definition, really the only way to share an Option type is by a field in a table. Any other way is a re-declaration which easily gets out of sync on changes to the code.
  • easy-navieasy-navi Member Posts: 31
    Thanks for your answers:). I have done it that way:
    Use Integer as a return value: then you can return 0 or 1 or 2 or 3 ...
    and next I have created next function TranslateCheckProtocol(Int : Integer) : Text[30]
    with the code
    IF Int=0 THEN EXIT('No')
     ELSE IF Int=1 THEN EXIT('yes')
      ELSE IF Int=2 THEN EXIT('Not applicable')
    

    So on the form you can make this that way:
    TextBox -> SourceExpr -> TranslateCheckProtocol(ChceckProtocolExists(Rec))
    (this is a textbox informing user what's happening)

    And on PostButton -> OnPush Trigger ->
    IF ChceckProtocolExists(Rec)=0 then Error('.....');
    \:D/
    http://www.reinwestuj.pl Inwestuj w nieruchomości. Condohotele, aparthotele.
  • jhoekjhoek Member Posts: 216
    Hmmm... Wondering how they managed to give function ConvertPostingType in table 5601 (in W1 2013 and W1 2013 R2) an Option return value?! Maybe it existed in the past, and the compiler accepts it, even though the designer doesn't list it?
    Kind regards,

    Jan Hoek
    Product Developer
    Mprise Products B.V.
Sign In or Register to comment.