img

Handles to objects

An object handle is a type that can hold a reference to an object. Handles allows you to create a number of variables that point to the same physical object in the memory. Using handles for primitive types is prohibited and is designed to work with objects that are created by the user.

To create a handle use the following syntax: user_type handle_ID @

 class MyObject { private int i; int getValue() { return i; } MyObject(int i) { this.i = i; } } // main function, calling every tick int Run() { MyObject obj(10); [email protected] obj_h; @obj_h = obj; System.Print("obj.getValue()="+obj.getValue()); System.Print("obj_h.getValue()="+obj_h.getValue()); return(0); } 

Output:

 obj.getValue()=10; obj_h.getValue()=10; 

For verification whether handles point to the same object or not, you can use the operator is as follows:

 class MyObject { private int i; int getValue() { return i; } MyObject(int i) { this.i = i; } } int Run() { MyObject obj1(10); [email protected] objX_h; [email protected] objY_h; @objX_h = obj1; @objY_h = obj1; if(objX_h is objY_h) System.Print("objX_h and objY_h point to the same object"); else System.Print("objX_h and objY_h point to different objects"); MyObject obj2(10); @objY_h = obj2; if(objX_h is objY_h) System.Print("objX_h and objY_h point to the same object"); else System.Print("objX_h and objY_h point to different objects"); return(0); } 

An object's life time is usually limited by the execution time of the code scope where they are declared. However, in case the object is referenced and that handle is used outside of the block, the object continues to exist as long as the handle is not assigned to null or until the code outside the handle scope is reached.