About user-defined functions
The PowerScript language has many built-in functions. But
you may find that you need to code the same procedure over and over
again. For example, you may need to perform a certain calculation
in several places in an application or in different applications.
In such a situation, you will want to create a user-defined
function to perform the processing.
A user-defined function is a collection of PowerScript statements
that perform some processing. After you define a user-defined function
and save it in a library, any application accessing that library
can use the function.
Two kinds
There are two kinds of user-defined functions:
- Global
functions, which are not associated with any object
in your application and are always accessible anywhere in the application.
These correspond to the PowerBuilder built-in functions that
are not associated with an object, such as the mathematical and
string-handling functions. You define global functions in the Function painter. - Object-level functions, which
are defined for a window, menu, user object, or application object.
These functions are part of the object’s definition and
can always be used in scripts for the object itself. You can choose
to make these functions accessible to other scripts as well.
These functions correspond to built-in functions that are
defined for specific PowerBuilder objects such as windows or controls.
You define object-level functions in a Script view for the object.
Deciding which kind you want
When you design your application, you need to decide how you
will use the functions you will define:
- If a function is general-purpose
and applies throughout an application, make it a global function. - If a function applies only to a particular kind
of object, make it an object-level function (you can still call
the function from anywhere in the application; it is just that the
function acts on a particular object type).
For example, suppose you want a function that returns the
contents of a SingleLineEdit control in one window to another window.
Make it a window-level function, defined in the window containing
the SingleLineEdit control. Then, anywhere in your application that
you need this value, call the window-level function.
Multiple objects can have functions with the same name Two or more objects can have functions with the same name
that do different things (in object-oriented terms, this is called polymorphism).
For example, each window type can have its own Initialize function,
which performs processing unique to it.
There is never any ambiguity about which function is being
called, because you always specify the object’s name when
you call an object-level function.