Object class in a Table class

Kelly Burgess kellyb at montana.com
Wed Nov 23 23:56:30 UTC 2022

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.


More information about the omnisdev-en mailing list