Object class in a Table class

Martin Obongita martin.obongita at yahoo.com
Thu Nov 24 13:25:54 UTC 2022

 Hi @Kelly and @Phil and @Doug,

The object reference did the trick.
Omnis crashing when an object instance in a table super class is called, is now sorted.
Thank you so much.
Lesson I have learnt is I should use instance variables sparingly.
It's cool to know there are beautiful people out in space ready and willing to throw in a lifesaver.


    On Thursday, November 24, 2022 at 02:55:19 AM GMT+3, Kelly Burgess <kellyb at montana.com> wrote:  
 Hi Martin,

>I have never used object reference in my code.
>This is because I don't understand how to use them.
>I use instance variables of type object, sub type object class created in table classes, to call methods in object classes.

The way you do it now is you create an instance var of type Object and you assign the subtype to be your object class.

To change over to an Object Reference, you just change the variable type to Object Reference.  You leave the subtype as it is, but for an object reference, the subtype is only used to tie the variable to an object class for purposes of using the Interface Manager - so it can show you the class's methods and properties.  Assigning the subtype is optional - you don't have to commit to using the Object Reference with a particular class.

The second thing you need is a line in your $construct to create the object instance.  With your current approach, the instance is created the first time the variable is referenced during code execution.  With an object reference, the instance is created when you execute a $newref().

    Calculate myObjRef as $clib.$objects.oMyObjClass.$newref()

The third thing you need is code in your $destruct to clean up the object.

    If myObjRef.$validref()
        Do myObjRef.$deleteref()
    End If

Omnis has improved automatic garbage collection in the last few years, so maybe that $destruct cleanup is no longer needed, but it's good practice.

Object references are used just like item references, so you don't need to change any of your code that uses the variable.  The problem with item refs is that sometimes when you pass one to another method, a new instance gets created and you lose whatever context the original instance carried with it.  Object references can be passed around, kept in lists, etc. without that worry.


Manage your list subscriptions at https://lists.omnis-dev.com
Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com 

More information about the omnisdev-en mailing list