Optional parameter in an Operation

Author: hugo.garcia@virtualage.com.br (hugogarcia)

Hello, Is it possible to create an optional parameter for an Operation? Where I could or not send the value for the parameter. For example: public operation unifaceOperation   params     string OptionalParam : IN  ;an optional parameter with some default value   endparams end

6 Comments

  1. Hi Hugo, uniface doesn't have a concept for optional parameters. To build some mechanism which comes next to optional parameters, uniface experts use a single list as a parameter which may have items, but there's no need to add them. You can query if a list has a specific key to detect if optional parameters have been included: if $item("OptionalParam",inList) != "") ... ; optional parameter available Greetings from Frankfurt/Germany, Uli


    Author: ulrich-merkel (ulrichmerkel@web.de)
  2. This is an example based on 9.6.04   entry do_p_start ; put YOUR code here ; if you need input or output, there is the editbox X_AFIELD already call OptionalParams(X_AFIELD) end ; do_p_start entry FILL_X_AFIELD X_AFIELD = "Mandatory=Mandat·;Optional=Option" end ; entry FILL_X_AFIELD entry OptionalParams ; using a list to support optional parameters params string vInList : IN ; list of parameters endparams call add_PROTOCOL("call OptionalParams(%%vInList%%%)") if ($item("Mandatory",vInList) = "") call add_PROTOCOL("ERROR: Parameter Mandatory is missing (or empty)") endif if ($item("Optional",vInList) = "") call add_PROTOCOL("Info: Parameter Optional is missing (or empty)") else call add_PROTOCOL("Info: Parameter Optional found as %%$item("Optional",vInList)%%%") endif end ; entry OptionalParams    

     

    Author: ulrich-merkel (ulrichmerkel@web.de)
  3. ulrich-merkel said This is an example based on 9.6.04   entry do_p_start ; put YOUR code here ; if you need input or output, there is the editbox X_AFIELD already call OptionalParams(X_AFIELD) end ; do_p_start entry FILL_X_AFIELD X_AFIELD = "Mandatory=Mandat·;Optional=Option" end ; entry FILL_X_AFIELD entry OptionalParams ; using a list to support optional parameters params string vInList : IN ; list of parameters endparams call add_PROTOCOL("call OptionalParams(%%vInList%%%)") if ($item("Mandatory",vInList) = "") call add_PROTOCOL("ERROR: Parameter Mandatory is missing (or empty)") endif if ($item("Optional",vInList) = "") call add_PROTOCOL("Info: Parameter Optional is missing (or empty)") else call add_PROTOCOL("Info: Parameter Optional found as %%$item("Optional",vInList)%%%") endif end ; entry OptionalParams    
     
      

    Thanks for your help!!


    Author: hugogarcia (hugo.garcia@virtualage.com.br)
  4. In case we have to handle even "empty" parameters as valid, it just takes a little helper function (9.6.04):   entry isKeyInList ; helper function when representation is empty returns boolean params string p_key : IN string p_list : IN endparams variables string v_void endvariables getitem/id v_void, p_list, p_key return($status > 0) end ; entry isKeyInList entry do_p_start ; put YOUR code here ; if you need input or output, there is the editbox X_AFIELD already call OptionalParams(X_AFIELD) end ; do_p_start entry FILL_X_AFIELD X_AFIELD = "Mandatory=·;Optional=Option" end ; entry FILL_X_AFIELD entry OptionalParams ; using a list to support optional parameters params string vInList : IN ; list of parameters endparams call <C4C_prefix>add_PROTOCOL("call OptionalParams(%%vInList%%%)") if (!isKeyInList("Mandatory",vInList)) call <C4C_prefix>add_PROTOCOL("ERROR: Parameter Mandatory is missing") endif if (!isKeyInList("Optional",vInList)) call <C4C_prefix>add_PROTOCOL("Info: Parameter Optional is missing") else call <C4C_prefix>add_PROTOCOL("Info: Parameter Optional found as %%$item("Optional",vInList)%%%") endif end ; entry OptionalParams


    Author: ulrich-merkel (ulrichmerkel@web.de)
  5. I consider these "little helper functions" counterproductive, hence stupid. Good practice is to avoid such functions (in Uniface). Just consider this: - why using function for something, that Uniface has a native statement for? - just use the statement (getitem/id) - Uniface passes all arguments by value (except structs) - so fort huge lists its just a waste of (CPU) time and memory - Uniface builds index for lists, but when you pass that list as string to another procedure (function), it's a different list and new index must be built - again just waste of (CPU) time and memory - the list is parsed and index built with every single call of this function (disclaimer: hope this is right, may some Uniface insider correct it, should it be wrong) - the resulting code is less human readable - you have to known, what that "helper" function does - you should test the result of calling the function (it may fail) All in all... it's much easier to just use Uniface native statement. Be aware of using such statements like ... if ($item("ID", V_LIST) == "")... this is tricky, since you are unable to distinguish "empty value" from "ID is no in the list"... therefore it's better to use getitem/id and test the $status By the way, in that Uli's example I'm not sure that "C4C_prefix" is. :-) Just my 2 cents, hope it helps a bit. Zdeněk


    Author: sochaz (zdenek.socha@fullsys.cz)
  6. sochaz said - why using function for something, that Uniface has a native statement for? - just use the statement (getitem/id)

    Hi Zdeněk, if you examine the implementation of the helper function "isKeyInList", you will find that this just returns wether the $status after the getitem/id is greater null. Returning a boolean allows to place isKeyInList(...) directly in an if statement makes it easier to read: if (!isKeyInList(“Mandatory”,vInList)) Unfortunately it takes a void variable for the getitem, but we are not interested in the contents. So if one just needs to know if a key is in a list, uniface has no native statement to do so.   P.S. <C4C_prefix> is a precompiler defined constant


    Author: ulrich-merkel (ulrichmerkel@web.de)