Optional parameter in an Operation | Uniface Development | Forum

Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

—  Results per page  —








— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters
For a group of consecutive words like 'end of support' use Match phrase

sp_Feed Topic RSS sp_TopicIcon
Optional parameter in an Operation
09 Nov 2017
6:48 pm
Avatar
hugogarcia
Member
Forum Posts: 6
Member Since:
29 Sep 2017
sp_UserOfflineSmall Offline

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

10 Nov 2017
7:36 am
Avatar
ulrich-merkel
Frankfurt/Germany
Member
Forum Posts: 1890
Member Since:
01 Oct 2012
sp_UserOfflineSmall Offline

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

10 Nov 2017
9:08 am
Avatar
ulrich-merkel
Frankfurt/Germany
Member
Forum Posts: 1890
Member Since:
01 Oct 2012
sp_UserOfflineSmall Offline

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

 

 

 
10 Nov 2017
12:32 pm
Avatar
hugogarcia
Member
Forum Posts: 6
Member Since:
29 Sep 2017
sp_UserOfflineSmall Offline

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!!

10 Nov 2017
7:25 pm
Avatar
ulrich-merkel
Frankfurt/Germany
Member
Forum Posts: 1890
Member Since:
01 Oct 2012
sp_UserOfflineSmall Offline

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

25 Jan 2018
10:04 am
Avatar
sochaz
Member
Forum Posts: 173
Member Since:
01 Oct 2012
sp_UserOfflineSmall Offline

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

25 Jan 2018
7:31 pm
Avatar
ulrich-merkel
Frankfurt/Germany
Member
Forum Posts: 1890
Member Since:
01 Oct 2012
sp_UserOfflineSmall Offline

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

Forum Timezone: Europe/Amsterdam

Most Users Ever Online: 131

Currently Online:
27 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

ulrich-merkel: 1890

Iain Sharp: 680

Theo Neeskens: 371

gianni: 357

Adrian Gosbell: 318

istiller: 299

rogerw: 272

Knut: 229

lalitpct: 197

Arjen van Vliet: 184

Member Stats:

Guest Posters: 3

Members: 10982

Moderators: 0

Admins: 6

Forum Stats:

Groups: 1

Forums: 62

Topics: 2323

Posts: 10052

Newest Members:

greentipodent1976, NicolasHib, gipipufen1981, Alfredvax, Lesterpioto, crystalst3, otogsa12, Ramonmaw, harolddz1, Lariskawo

Administrators: admin: 23, diseli: 1045, Nico Peereboom: 84, richiet: 406, Mike Taylor: 38, JanCees: 39