Self Reference with Functional Classes in Gupta

With Gupta Team Developer you can create functional class datatypes (UDV) – comparable to classes from C# or Java. In contrast to the above frameworks with Gupta variables that are based on these data types are not only declared but also instantiated. This has a great positive effect: it avoids the runtimeerror „NullReferenceException“ which is quite common in C# and Java.

Sample C#

class SomeClass
{
        public Int32 someMember;
}
public void SomeMethod()
{
    SomeClass someObject;
        // NullReferenceException
    someObject.someMember = 7;

        // this works
    someObject = new SomeClass();
    someObject.someMember = 7;

}

Sample Gupta

Functional Class: SomeClass
    Instance Variables:
        Number: nSomeMember

Function: SomeFunction
    Local variables:
        SomeClass: someObject
    Actions:
        ! this works
        Set someObject.nSomeMember = 7
At the moment when the function SomeFunction() is called Gupta instantiates the class.

But this default behavior has also a disadvantage: the class (UDV) SomeClass cannot contain itself (easily) as an instance variable:

Functional Class: SomeClass
 Instance Variables:
     ! results in compile error: Circularly defined class.
     SomeClass: cSomeChild
Because as soon as SomeClass would be instantiated the instance variable cSomeChild would also be instantiated then their instance variable cSomeChild and so on.

The same problem occurs if Class1 is an instance variable of type Class2 and Class2 of Class1.

But there is an almost not documented way to create just such constructs: by setting the UDV to OBJ_Null when declaring it:

Functional Class: SomeClass
 Instance Variables:
     SomeClass: cSomeChild = OBJ_Null
A few comments:
  • I have discovered this option randomly in a single place in the help file of Gupta: as sample code in the description of the command SalObjIsNull.
  • Only UDVs can be set in the declaration section. For Number, String, DateTime, etc. TD unfortunately does not allow it. After all a constructor function for UDVs should come with TD 6.3 where you could set values for instance variables.
  • If you want to use the UDV cSomeChild you previously have to either assign an existing instance of the same or derived type or create an instance via the new command:
Set cSomeChild = new SomeClass

Examples of use

There are a lot of scenarios where it is useful or even necessary to use nested classes:

  • Mapping of hierarchical data structures such as XML documents or even all data structures that can be displayed in TreeViews that have more than one hierarchy level.
  • Double linked list structures
  • publish / subscriber classes which are mutually linked
  • tree structures such as red-black-index trees

Have fun experimenting with these structures.

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: