Null constants

Share this topic:



Link to this posting

Postby Ursego » 19 Feb 2013, 22:21

Have a ready set of nulls (of different data types) instead of using SetNull() each time you need a variable with null.

Sometimes we use variables, which are set to null - for example, to pass null to a function, or, oppositely, to return null from a function. Usually developers declare a variable and nullify it with SetNull() in the same script where they are used, but these two lines (really, hundreds lines all over the project!) can be avoided if you declare constants of different data types in a globally-accessed object. Technically, PowerBuilder doesn't allow to initialize constants with NULL, so we cannot use the method, described in the topic "Constants used all over the application" (i.e. to create an object n_null with a set of NULL constants). Instead, we will utilize any class, a global object of which is created in the application (an object, providing different technical services - like n_util - is an ideal candidate) and declare a set of public instance variables using privatewrite access modifier. Then we will nullify them using SetNull() in the Constructor event. Thus, we will have physical variables which logically acts as constants.

So, the first step is to declare the NULL vars in the instance variables declaration section of the selected class:

Code: Select all
public:

   // NULLs of different data types:
   privatewrite boolean     NULL__BOOLEAN
   privatewrite int         NULL__INT
   privatewrite long        NULL__LONG
   privatewrite dec         NULL__DEC
   privatewrite string      NULL__STRING
   privatewrite char        NULL__CHAR
   privatewrite date        NULL__DATE
   privatewrite time        NULL__TIME
   privatewrite datetime    NULL__DATETIME

The second step - to nullify them in the NVO's Constructor script:

Code: Select all
SetNull(NULL__BOOLEAN)
SetNull(NULL__INT)
SetNull(NULL__LONG)
SetNull(NULL__DEC)
SetNull(NULL__STRING)
SetNull(NULL__CHAR)
SetNull(NULL__DATE)
SetNull(NULL__TIME)
SetNull(NULL__DATETIME)

So, instead of

Code: Select all
string ls_null
SetNull(ls_null)
return ls_null

we will write

Code: Select all
return n_util.NULL__STRING
User avatar
Ursego
Site Admin
 
Posts: 112
Joined: 19 Feb 2013, 20:33

Return to Elegant Code

Who is online

Users browsing this forum: No registered users and 1 guest


Null constants

Share this topic:


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





free counters

eXTReMe Tracker