October 12, 2005

Global Constant-Package

When you need literals and constants in forms, then the easiest way is to hardcode it in the sourcecode.

But please never do it the easiest way !

Have a look at this source-snippets and get a feeling, how to use them in your own code :

All my forms have attached libraries, in which I store my framework-functions.

One package spec. in this library is the package CONST. This package has no package body.


PACKAGE Const IS
/*
|| Name : Const
||
|| Function : This package contains all global Constants
||
|| Author : 08.08.1999, VOL
|| Updates :
*/

-- Alert
alr_Error CONSTANT VARCHAR2 (30) := 'AL_ERROR';
alr_Info CONSTANT VARCHAR2 (30) := 'AL_INFO';
alr_Warning CONSTANT VARCHAR2 (30) := 'AL_WARNING';

-- Mode
mod_Call_Form CONSTANT VARCHAR2 (10) := 'CALL_FORM';
mod_Open_Form CONSTANT VARCHAR2 (10) := 'OPEN_FORM';
mod_Go_Form CONSTANT VARCHAR2 (10) := 'GO_FORM';
mod_New_Form CONSTANT VARCHAR2 (10) := 'NEW_FORM';

-- Block, Record and Form-Status
sta_Changed CONSTANT VARCHAR2 (10) := 'CHANGED';
sta_Insert CONSTANT VARCHAR2 (10) := 'INSERT';
sta_New CONSTANT VARCHAR2 (10) := 'NEW';
sta_Query CONSTANT VARCHAR2 (10) := 'QUERY';

-- Other
CR CONSTANT VARCHAR2 (1) := CHR (10);
Tab CONSTANT VARCHAR2 (1) := CHR (9);
TRUE CONSTANT VARCHAR2 (10) := 'TRUE';
FALSE CONSTANT VARCHAR2 (10) := 'FALSE';

END Const;

This is only a very short part of my complete Const-package

All sourcecodes in which I now need a literal I change this to a constant from this package

e.g.

IF NAME_IN ('System.Last_Record') = Const.TRUE THEN
--do something;
ELSE
--do something different;
END IF;


This method of using a const-package can be enhanced, if you create another const-package in each form. Name it const_local.


PACKAGE Const_local IS
/*
|| Name : Const_local
||
|| Function : local Constant-Package
||
|| Author : 08.08.1999, VOL
|| Updates :
*/

-- Blocks
blk_Control CONSTANT VARCHAR2 (30) := upper ('Control');

-- Items
itm_Test CONSTANT VARCHAR2 (61) := upper ('Control.TI_TEST');

END Const_local;


With this local package you can now handle all the literals which are needed only in the form. This package can rapidly grow and is the central place for all other functions and procedures.

These packages offer some very important opportunities. All your literals are now constants. That means you have never runtime-errors, when you write a typo. You now have always compile-time-errors.

If you write a literal incorrect, than there is only one place in the const-package, where the constant is defined and the error can occur. Those error may occur, but they are found very fast and can be corrected directly.


Conclusion: Use the global Const-package in a library and use the local-package for all needs you have in the form itself.

Try and use it

2 comments:

Anonymous said...

I really liked this one:

TRUE CONSTANT VARCHAR2 (10) := 'TRUE';
FALSE CONSTANT VARCHAR2 (10) := 'FALSE';

vimal said...

You have replied in one of my thread in oracle forms forum that to copy webutil.plx to the directory where webutil.pll exists. Sir how can I get a .plx file.I mean how to compile a .pll file