Working with objects

This topic introduces to basic object usage in Genero BDL.

In order to use an object in your program:

  1. define an object variable using the class identifier.
  2. instantiate the object; this is usually done by invoking a class method.
  3. call object methods to manipulate the created object.
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.

You do not have to destroy objects. This is done automatically by the runtime system for you, based on a reference counter.
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.
You can pass object variables to functions or return them from functions. Objects are passed by reference to functions. In this example, the function creates the object and returns its reference on the stack:
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.

Example

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