Omnis Technical Note TNOO0001 October 2006

Objects and Object References

for Omnis Studio 4.1 or after.
by Götz Krija

There are two different ways to create an object instance, in fact by:

Creating a variable of the type Object and then choosing the actual object class as the subtype. The object instance will then be created automatically at runtime

Creating a variable of the type Object without a subtype and then using the following code:

Do $objects.oTest.$new() Returns ivTestObj

When using several object instances of one class you would have to create a variable for each instance (ivTestObj1, ivTestObj2, and so on). For 2 or 3 instances this shouldn┬ąt be a problem, but it would get difficult if you had more than 3 instances, and often it is not known beforehand how many instances in total you will need.

The solution is to store object instances dynamically in a list. Then a single object variable is sufficient.

Do ivObjectInstanzList.$define(ivTestObj)
Do ivObjectInstanzList.$add($objects.oTest.$new())
Do ivObjectInstanzList.$add($objects.oTest.$new())
...

With ivObjectInstanzList.2.1.$test(), the method $test() of the second object instance could be executed. If these instances are used again in a different place, e.g. in another window instance, you can assume that the list passed over would be identical to the original list.

Do $windows.$open ('w2',kwindownormal,ivObjectInstanzList)

Please note that in this case real object instances are not passed but rather copies of these instances are created. A remedy can be found by using the variable type Object Reference, introduced in Omnis Studio 4.1.

Do $objects.oTest.$newref() Returns ivTestObjRef

The above method creates a new instance of the class oTest and stores it somewhere in memory. At the same time, a Object Reference is returned (held in ivTestObjRef) that points to the object instance in memory. Please note that this variable is the only connection to the real object instance and that you can access the methods of this instance only via this reference. This also means that the 'real' object instance can only be destroyed via the reference. In previous versions of Omnis (before object references were available), object instances would remain in memory and were not destroyed until after the library was closed.

You can destroy the object references with the method:

Do ivTestObjRef.$deleteref()

If you return to the original idea, that is, storing object instances in lists and passing them to a second window, the code will look as follows by using object references:

Do ivObjectInstanzList.$define (ivTestObjRef)
Do ivObjectInstanzList.$add ($objects.oTest.$newref())
Do ivObjectInstanzList.$add ($objects.oTest.$newref())
...

The list can be passed to a second window without creating copies of the object instances.

Do $windows.$open('W2',kwindownormal,ivObjectInstanzList)

With ivObjectInstanzList.2.1.$test() you could now, as before, execute the method $test() of the second object instance.