Navigation:  Tutorial >

Dynamic Classes and Structures - Overview

Previous pageReturn to chapter overview

ot4xb.ch provide a set of commands to allow the definition of the class function for dynamic classes and gwst structure classes in a simple way.

 

Dynamic Class Example:

 

BEGIN DYNAMIC CLASS MyClass FROM MyParentClass

   DYNAMIC CLASS VAR  nClassVar

   DYNAMIC VAR        cInstanceVar

   DYNAMIC READONLY PROPERTY lEmpty ;

                    BLOCK {|Self| Empty( ::cInstanceVar)}

   DYNAMIC METHOD init BLOCK {|Self| ::cInstanceVar := "Hello" }

END DYNAMIC CLASS

 

Gwst Structure Class Example

 

BEGIN STRUCTURE RECT

   MEMBER LONG left

   MEMBER LONG top

   MEMBER LONG right

   MEMBER LONG bottom

END STRUCTURE

 

 

Gwst Structure classes are also Dynamic classes, with a small extra initialization, this mean that you can use the Dynamic Class commands into your Gwst structure declaration

 

BEGIN STRUCTURE RECT

   MEMBER LONG left

   MEMBER LONG top

   MEMBER LONG right

   MEMBER LONG bottom

   // --------------------

   DYNAMIC PROPERTY width  READ  ( ::right -  ::left );

                           WRITE ( ::right := ::left + v)

   // --------------------

   DYNAMIC PROPERTY height READ  ( ::bottom -  ::top );

                           WRITE ( ::bottom := ::top + v)

   // --------------------

END STRUCTURE

 

Ot4xb Dynamic Class and Structure commands are just wrappers over the ot4xb TXbClass() helper class, that probably will save you a lot of work creating this kind of classes. But there are situations where you may want to use the TXbClass() methods directly . See the following Sample

 

// -----------------------------

CLASS TDbDictionary

// ..... add here the dictionary class stuff

ENDCLASS 

// -----------------------------

CLASS TDatabase

// ..... add here the base class stuff

ENDCLASS 

// -----------------------------

function CreateDbClass( cClsName , oDataDict )

local oClsObj := ClassObject( cClsName )

local oXbCls

local n,nFields

local cBlk := "{|s,v| iif(PCount() > 1, s:FPut(%i,v) , s:FGet(%i) )}" 

if oClsObj == NIL 

   oXbCls := TXbClass():New(cClsName)

   oXbCls:Parent("TDatabase")

   oXbCls:_SharedClassVar( "oDataDic" )

   nFields := oDataDic:FCount()

   for n := 1 to nFields

      oXbCls:_Property( oDataDic:FName(n) ,cPrintf(cBlk,n,n))

   next

   oClsObj := oXbCls:Create()

   oXbCls:Destroy() // CleanUp the TXbClass internal buffers

   oClsObj:oDataDic := oDataDic // initialize the class var with our 

                                // data dictionary object

endif

return oClsObj