nvl()

Share this topic:



Link to this posting

Postby Ursego » 19 Feb 2013, 21:33

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.
User avatar
Ursego
Site Admin
 
Posts: 111
Joined: 19 Feb 2013, 20:33

Link to this posting

Postby benconsult » 27 Apr 2015, 21:06

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
else
return aa_variable
end if
end function
benconsult
 
Posts: 2
Joined: 27 Apr 2015, 20:59

Link to this posting

Postby Ursego » 12 May 2015, 14:37

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.
User avatar
Ursego
Site Admin
 
Posts: 111
Joined: 19 Feb 2013, 20:33

Link to this posting

Postby benconsult » 13 May 2015, 18:16

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.
benconsult
 
Posts: 2
Joined: 27 Apr 2015, 20:59

Link to this posting

Postby Ursego » 24 May 2015, 07:59

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:
User avatar
Ursego
Site Admin
 
Posts: 111
Joined: 19 Feb 2013, 20:33


Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 1 guest


nvl()

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





free counters

eXTReMe Tracker