Back to Contents


Classes and Objects

Summary:

See also: Variables, Functions.


Introduction

Classes and objects introduce the benefits of OOP programming in Genero BDL.

Classes are templates which define methods that interact with a specific program object instantiated at runtime, allowing you to change the appearance or behavior of the object.

Classes are grouped into packages. Some packages are build-in in the runtime system, other packages are available as extension libraries and must be imported to be available to the program.


Syntax

Syntax 1: Defining an object

DEFINE obj package.classname

Syntax 2: Using a class method

CALL package.classname.method( parameter [,...] ) [RETURNING ...]

LET variable = package.classname.method( parameter [,...] )

Syntax 3: Using an object method

CALL obj.method( parameter [,...] ) [RETURNING ...]

LET variable = obj.method( parameter [,...] )

Notes:

  1. obj is the name of the variable holding a reference to the object.
  2. package is the name of the package the class comes from.
  3. classname is the name of the built-in class.
  4. method is the name of the method.
  5. parameter is a parameter for the method.

Usage

Class and Object Methods

There are two types of methods: Class Methods and Object Methods. Methods can be invoked like global functions, by passing parameters and/or returning values.

Class Methods  - you call these methods using the class identifier (package name + class name) as the prefix, with a period as the separator.

01 CALL ui.Interface.refresh()

The method refresh() is a Class Method of the Interface class, which is part of the ui package.

Object Methods - To use these methods, the object must exist. After an object has been created, you can call the Object Methods in the class by using the object variable as a prefix, with a period as the separator:

01 LET b = n.getDocumentElement()

The method getDocumentElement() is an Object Method of the class to which the n object belongs. 

Working with Objects

To handle an object in your program, you

O1 DEFINE n om.DomDocument, b DomNode
02 LET n = om.DomDocument.create("Stock")
03 LET b = n.getDocumentElement()

The object n is instantiated using the create() Class Method of the DomDocument class. The object b is instantiated using  the getDocumentElement() Object method of the DomDocument class. This method returns the DomNode object that is the root node of the DomDocument object n.

The object variable only contains the reference to the object. For example, when passed to a function, only the reference to the object is copied onto the stack.

You do not have to destroy objects. This is done automatically by the runtime system for you, based on a reference counter.

01 MAIN
02    DEFINE d om.DomDocument
03    LET d = om.DomDocument.create("Stock")  -- Reference counter = 1
05 END MAIN  -- d is removed, reference counter = 0 => object is destroyed.

You can pass object variables to functions or return them from functions. Objects are passed by reference to functions. In the following example, the function creates the object and returns its reference on the stack:

01 FUNCTION createStockDomDocument( )
02    DEFINE d om.DomDocument
03    LET d = om.DomDocument.create("Stock")  -- Reference counter = 1
04    RETURN d
05 END FUNCTION  -- Reference counter is still 1 because d is on the stack

Another part of the program can get the result of that function and pass it as a parameter to another function.

Example:

01 MAIN
02    DEFINE x om.DomDocument
03    LET x = createStockDomDocument( )
04    CALL writeStockDomDocument( x )
05 END MAIN
06
07 FUNCTION createStockDomDocument( )
08    DEFINE d om.DomDocument
09    LET d = om.DomDocument.create("Stock")  
10    RETURN d
11 END FUNCTION
12
13 FUNCTION writeStockDomDocument( d )
14    DEFINE d om.DomDocument
15    DEFINE r om.DomNode
16    LET r = d.getDocumentElement()
17    CALL r.writeXml("Stock.xml")
18 END FUNCTION

Built-in packages

Built-in packages implement classes that are useful for any Genero program, as for example string manipulation classes.

Built-in packages are part of the runtime system library and do not need any import instruction to be available.

Package: base

Classes Purpose
Application Provides an interface to the application internals
Channel Provides basic read/write functionality (files/communication)
StringBuffer Allows manipulation of character strings
StringTokenizer Allows parsing of strings to extract tokens
TypeInfo Provides serialization of program variables

Package: ui

Classes Purpose
Interface Provided to manipulate the user interface
Window Provides an interface to the Window objects
Form Provides an interface to the forms used by the program
Dialog Provides an interface to the interactive instructions
ComboBox Provides an interface to the ComboBox formfield view

Package: om

Classes Purpose
DomDocument Provides methods to manipulate a DOM data tree
DomNode Provides methods to manipulate a node of a DOM data tree
NodeList Holds a list of selected DomNode objects
SaxAttributes Provides methods to manipulate XML element attributes
SaxDocumentHandler Provides methods to write an XML filter
XmlReader Provides methods to read and process a file written in XML  format
XmlWriter Provides methods to write XML documents to different types of output

Extension Library Packages

Packages that must be imported are typically implementing specific utility classes that are not needed by regular Genero programs.

In order to use a class of an extension library package, you must import the package library in your program.

Package: util

Classes Purpose
Math Provides an interface for mathematical functions

Package: os

Classes Purpose
Path Provides functions to manipulate files and directories on the machine where the BDL program executes