Tips   >   Objectclasses   >   Object Classes
When I started learning Omnis Studio, I disliked object classes and couldn't see how they were supposed to be better than code classes. Code classes seemed so much easier to create, call, and use. After a year of working without object classes I attended my first Omnis conference and was enlightened by Geir Fjaerli on the beauty of object classes. His words "Anything you can do with a code class, you can do better with an object class" has proved true many times over.
Now object classes are my favorite!There are several advantages that object classes have over code classes.
Omnis has done a fantastic job on the developer interface for object classes.
When you need to use an object class.
If you use good consistent naming conventions for your parameters, it will be very obvious to you (and others) what each parameter means. (See
You can use the ) to drag and drop methods from other types of classes, but you can't create a variable and right click on it, the way you can with object classes. (You can for table classes as of Omnis Studio v4.2)Omnis Studio v4.1 introduced the Object reference variable type. This gives you two options for the variable
:When you use
as the variable , Omnis Studio automatically creates an instance of the object class the first time you send a message to the object. The object instance is bound to the variable and is automatically closed when the variable is closed. If a local variable was used for the object instance, the object instance closes when the method is finished. The trouble with type instances is that it is very difficult to pass and store references to them in other instances. What often happens is that you end up with a new instance, rather then a reference to the original instance. To solve this problem Omnis Studio introduce the variable type.When you use $newref to create an instance of the object class.
as the variable , you must useDo $clib.$objects.ObjectClassName.$newref() Returns oObjRef
oObjRef is a pointer to the object instance. The instance is given a life of its own in memory. You can now pass around and make as many copies of oObjRef as you like. Everything points to the original instance of ObjectClassName. Even though oObjRef might be a local variable, the object instance will continue to live on after the method execution is finished. The object instance lives as long as the task which contains it is open. This makes it possible (and easy) to have several windows point to the data in a single object instance. Changing the data in the object instance will be reflected in all the windows which point to that instance. There is a downside to object instances... you are responsible to delete the object instance if and when you are done with it. You use the $deleteref method to close the instance.
Do oObjRef.$deleteref()
If you forget to delete the object instances that you no longer need, and keep opening new ones, significant memory and resource leaks will occur.
You can get a list of all $listrefs.
instances usingDo $listrefs() Returns List
You can also check if an $validref.
is valid usingIf oObjRef.$validref()
You can copy a $copyref
object instance usingCalculate oObjRef2 as oObjRef.$copyref()
David Swain has written some excellent Omnis Technical Newsletters on Object Classes and Object vs. Object Reference variable types. www.omnis.net/technews/You can call an object class method in a single line, without creating an object type variable.
The syntax for doing this is as follows:
Do $libs.LibName.$objects.ClassName.$new().$MethodName(Parameters) Returns Value
This single line of code will instantiate the object, call the method, return the value, and then close the object instance. Omnis Studio evaluates the first part up to (), then calls the method in the object, sending along the parameters if any.
Do and Calculate are equivalent, so the following line of code, though different in format is exactly the same as the previous "Do" statement.
Calculate Value as Do $libs.LibName.$objects.ClassName.$new().$MethodName(Parameters)
If you have an object type variable which has the subtype empty (not pointed to an object) you can set the
subtype using the following syntax:
Do $libs.LibName.$objects.ClassName.$new() Returns oObjectVariable
Once you've set the object type variable's subtype you can use the variable the way you normally would.
Do oObjectVariable.$MethodName(Parameters) Returns Value
You don't have to use objects on-the-fly. I use them in situations where the default object class might be substituted with a different object class. For example I may switch the object class I'm using inside a table class depending on the data base the application is currently connected to, or in a multi-library application I might look for an object class in the $clib and point to it instead of the default object class in a base classes library.If you want to share an object instance with another class instance of any type, use the $newref.
variable and instantiate the object instance usingSee the topic
Instead of setting item references to the object instance, you can receive and copy the Object Reference. All the copies of the point to the same object instance.A singleton is an object that you want one and only one instance of. For more information on the Singleton Design Pattern see Wikipedia.
If you want a singleton of an Omnis Studio object class the following technique recommended by Bruno Del Sol works quite nicely.
You would add a method to your Startup_Task with a method name like $getSingleton.
The $getSingleton method would have the following code: (except for the DEMO ONLY lines)
; Check for any instances of the object.
If $clib.$objects.oSingleton.$insts.$count=0
; No instance was found. Open a $newref instance.
Do $clib.$objects.oSingleton.$newref() Returns ObjRef
; If you need to preset any properties in the singleton, you could do that here.
Do ObjRef.$initialize(Value1,Value2)
Else
; An instance was found. Get the object reference to the instance.
Calculate Row as $clib.$objects.oSingleton.$listrefs()
Calculate ObjRef as Row.C1
End If
; Send a $doSomething message to the single instance. (DEMO ONLY)
Do ObjRef.$doSomething() ;; DEMO ONLY
Quit method ObjRef
To use the singleton you would do the following:
; Get a reference to the singleton
Do $ctask.$getSingleton() Returns ObjRef
If ObjRef.$validref
; Send a message to the singleton.
Do ObjRef.$doSomething()
End If
Click the Run Demo button in the window. There is a breakpoint in the demo code. You can step through the code.
The first time you run the demo the singleton instance will be opened using $newref. The second time you run the demo, the single instance will be found in the $listrefs and simply returned to you.