Advanced features / OOP support |
This topic introduces to basic object usage in Genero BDL.
In order to use an object in your program:
DEFINE n om.DomDocument, b DomNode LET n = om.DomDocument.create("Stock") 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.
MAIN DEFINE d om.DomDocument LET d = om.DomDocument.create("Stock") -- Reference counter = 1 END MAIN -- d is removed, reference counter = 0 => object is destroyed.
FUNCTION createStockDomDocument() DEFINE d om.DomDocument LET d = om.DomDocument.create("Stock") -- Reference counter = 1 RETURN d 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.
MAIN DEFINE x om.DomDocument LET x = createStockDomDocument() CALL writeStockDomDocument( x ) END MAIN FUNCTION createStockDomDocument() DEFINE d om.DomDocument LET d = om.DomDocument.create("Stock") RETURN d END FUNCTION FUNCTION writeStockDomDocument( d ) DEFINE d om.DomDocument DEFINE r om.DomNode LET r = d.getDocumentElement() CALL r.writeXml("Stock.xml") END FUNCTION