Page 1 of 1


PostPosted: 19 Feb 2013, 21:33
by Ursego
In contrast to iif(), which was stolen from VB, the next function - nvl() - is stolen from Oracle :lol: . It accepts 2 arguments. IF the first argument is not null THEN that argument's value is returned; OTHERWISE, the second argument's value is returned. Works with all datatypes. Examples of use:

Code: Select all
ll_elephants_count = nvl(uf_get_elephants_count(), 0) // in case uf_get_elephants_count() can return null

ls_full_name = ls_first_name + " " + nvl(ls_mid_name + " ", "") + ls_last_name // ls_mid_name is optional

ls_err_msg = "Argument as_mode contains invalid value " + nvl("'" + as_mode + "'", "NULL") + "." // prevent NULLifying of ls_err_msg

The function is overloaded for the following datatypes: string, long, double, boolean, PowerObject (overloading of global functions is described here). Of course, both the arguments and the returned value have a same datatype. In the next example, the integer variable ai_mode is cast to string:

Code: Select all
ls_err_msg = "Argument ai_mode contains invalid value '" + nvl(String(ai_mode), "NULL") + "'."

Save the file nvl.srf on your hard disk and import it into your application.

Re: nvl()

PostPosted: 27 Apr 2015, 21:06
by benconsult
Of course you could just use the any variable type for nvl, and not have to worry about overloading:

global function any nvl (any aa_variable, any aa_otherwise);

if isnull(aa_variable) then
return aa_otherwise
return aa_variable
end if
end function

Re: nvl()

PostPosted: 12 May 2015, 14:37
by Ursego
Unfortunately, it's not a good way - it allows to supply arguments of different datatypes with no compilation time error. In fact, the first version was with "any" arguments, but the described problem forced me to switch to downloading.

Re: nvl()

PostPosted: 13 May 2015, 18:16
by benconsult
It's no more dangerous than using dot notation to load a value from a datawindow into a variable without typechecking. If fact, it's probably less so, since most of the time using nvl, you would supply a literal, such as an empty string, or a zero, and so type mismatches would be obvious.

However, you can always make a function that's using an Any argument more robust by checking the classname to avoid runtime errors.

Re: nvl()

PostPosted: 24 May 2015, 07:59
by Ursego
benconsult wrote:It's no more dangerous than using dot notation
But it's no less dangerous!

By default, solutions with ANY datatape are less desireble. It's not only a matter of type safety but also of double-converting between types each time an ANY variable is used. Shortly - not elegant. :lol: