PBDR.COM

About   -   Contact   -   Purchase   -   Search   -   What's New

 
 
Enumerated Datatypes
PowerBuilder does not allow you to create your own enumerated datatypes. Although this is not a major problem, when you are building a framework and you want to offer a function that allow the developer slightly different options from the same function by passing in an integer then an enumerated Datatype would make the code much more readable and allow for future expansion and rearrangement of the options without difficult code changes.

In designing the new framework for my current client I wanted to use enumerated Datatypes to make the code more readable. I have been learning Java at home just to add another string to my bow and was looking at how they implement enumerated datatypes. It occurred to me that it would be possible to do the same thing in PowerBuilder.

Basically the way it works it that the related functions are in a class lets take an example from my new framework the Window Agent. One function of the Window Agent is to open a window, but the developers are allowed to open the window in may different ways with many different options; topleft, fullscreen, cascaded, originalsize... etc.

To create the enumerated Datatypes I declared a set of instance constant integers inside the class for each enumerated type. You can either declare them sequentially or if you want to allow for multiple options in a single call you can define them in binary sequence so they can be anded together:

PUBLIC:
Integer Constant ii_FullScreen = 1
Integer Constant ii_TopLeft = 2
Integer Constant ii_Cascaded = 3

An example function call would look like:

nc_agt_window lagt_win

lagt_win.Open( "windowtoopen", lagt_win.ii_FullScreen )

Although this is not ideal is makes the code much more readable than:

nc_agt_window lagt_win

lagt_win.Open( "windowtoopen", 1 )

And it also allow me to add more options and change the sequence or values of the constants and the developers code will work with no changes required.

Russ Hensel has sent in the following extension to this tip. The example shown above can also be written using the following code:

nc_agt_window lagt_win

lagt_win.Open( "windowtoopen", nc_agt_window.ii_FullScreen )

What did Russ change? He used the class name instead of the reference variable. You can do this globally without instantiating anything. This is because PowerBuilder knows the value of the constant at compile time.

This does not buy you a whole lot in this particular example but if you think about examples where you want to reference a constant when you do not have a reference to an object containing the constant then you do not have to create and destroy an object locally just to reference the enumerated constants.

 

Top of Page

Legal Notice

Ken Howe 2011