Null constants


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. So, we will have physical variables which practically work as constants.

Step 1: Declare the NULL vars in the instance variables declaration section of the chosen 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

Step 2: Nullify them in the NVO's Constructor:

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)

Now, instead of

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

we can simply write

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



Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!



cron
Traffic Counter

eXTReMe Tracker