Selfreference with Arrays of Functional Classes in Gupta

Recently I showed how to implement a Functional Class in Gupta that contains „itself“ as an Instance Variable. You just have to initialize it with OBJ_Null.

SomeClass: class1 = OBJ_Null

Martin asked me then if it is possible to have an array of Functional Classes of the same type as an Instance Variable. This kind datastructure is very useful e.g. if you want to represent a tree structure like a filesystem.

SomeClass: aClasses[*]

Unfortunately TeamDeveloper redeems that with an error while compiling: „Error: Circularly defined class.“ Martin found an artful approach to outsmart TD: First write SomeClass c = OBJ_Null afterwards replace = OBJ_Null through [*]. But that doesn’t work on my machine. It crashes when I access an element of the array.
Another solution to that problem is to define an additional listclass:
Based on a datatype (Functional Class) called „Node“ an additional class „ListNodes“ is created. This one contains an array of „Node“ as an Instance Variable and an internal counter:

Functional Class: ListNodes
 Instance Variables
  Node: __nodes[*]
  Number: __nCountNodes

This listclass can be used in class „Node“ as an Instance Variable when initialized with OBJ_Null:

Functional Class: Node
 Instance Variables
  ListNodes: __listChildNodes = OBJ_Null

If one adds some access functions to class „Node“ it becomes really useful:

 

Function: AddChildNode
 Returns
 Parameters
  Node: node
 Local variables
 Actions
  If ( __listChildNodes = OBJ_Null )
   Set __listChildNodes = new ListNodes
  Set __listChildNodes.aNodes[__listChildNodes.nCountNodes] = node
  Set __listChildNodes.nCountNodes = __listChildNodes.nCountNodes + 1
Function: CountChildNodes
 Returns
 Parameters
 Local variables
 Actions
  If ( __listChildNodes = OBJ_Null )
   Return 0
  Else
   Return __listChildNodes.nCountNodes
Function: GetChildNode
 Returns
  Node:
 Parameters
  Number: nIdx
 Local variables
 Actions
  If ( __listChildNodes = OBJ_Null )
   Return OBJ_Null
  Else
   If ( (nIdx >= 0) AND (nIdx < __listChildNodes.nCountNodes) )
    Return __listChildNodes.aNodes[nIdx]
   Else
    Return OBJ_Null
Function: RemoveChildNode
 Returns
 Parameters
  Number: nIdx
 Local variables
 Actions
  If ( __listChildNodes = OBJ_Null )
   ! do nothing
  Else
   If ( (nIdx >= 0) AND (nIdx < __listChildNodes.nCountNodes) )
    While ( nIdx < __listChildNodes.nCountNodes - 1)
     Set __listChildNodes.aNodes[nIdx] = __listChildNodes.aNodes[nIdx +1 ]
     Set nIdx = nIdx + 1
    Set __listChildNodes.aNodes[__listChildNodes.nCountNodes] = OBJ_Null
    Set __listChildNodes.nCountNodes = __listChildNodes.nCountNodes - 1
In the attachment is an example that reads the whole directory-structure of drive C: (it may run some time to gather all information).
DirectoryTree.zip
Happy Coding
Advertisements

Über thomasuttendorfer
Ich bin Entwicklungsleiter bei der Softwarefirma [ frevel & fey ] in München. Wir entwickeln Business-Software für Verlage und verwenden dafür den Gupta Team-Developer sowie Visual Studio.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

%d Bloggern gefällt das: