Built-in variables: rootthis

The variable rootthis refers to the application component, also known as the root object.


Core::Root rootthis


Each GUI application contains at least one application component, so called root object of the view tree. This component represents the entire screen area. Adding to it a new GUI component results in this component appearing on the screen. It is thus common to manage all top level GUI components like alert panels as views embedded directly within the application component. Doing this however in context of a component other than the application component itself requires first to obtain access to the instance of the application component - an access to the root object.

Before Embedded Wizard 12, the established approach to obtain an access to the root object was the method GetRoot(). As long as the method was invoked in context of a GUI component belonging already to the view tree, the approach worked as expected. It was however not very convenient and could lead to errors with objects not belonging to the view tree.

The in version 12 introduced new built-in variable rootthis provides a more reliable way to access the root object. Generally, the variable rootthis stores a reference to the actually existing root object. It can be evaluated within any Chora expression, also in context of a GUI component not belonging to the view tree or even in context of an ordinary object not acting as a view, for example in the implementation of Device Interface. The following code snippet demonstrates how the built-in variable rootthis is used to access the root object and e.g. present an alert component:

rootthis.PresentDialog( new SomeUnit::SomeAlert, null, null, null, null, null, null, null, null, false );

The built-in variable rootthis is declared with data type Core::Root which is the base class of all application components. The members of the Core::Root class can thus be accesed directly in context of rootthis. To access members implemented in your own application component, however, you will need to explicititly type cast the value rootthis to the class of the expected application component and test whether the type-cast was successful. For example:

// Access the 'root' object and type-cast it to the class of the application // component var Application::Application app = (Application::Application)rootthis; // If the type-cast was successful, the members of the application component // can be accessed. For example, you can invoke a method implemented in the // class 'Application::Application' if ( app ) app.SomeMethod(); // ... if 'rootthis' is not referring an instance of 'Application::Application' // nor of any descending classes, the type-cast will fail else trace "'rootthis' is not 'Application::Application";

If you intend to access members of your own implemented application component by applying the above mentioned type-cast operation, please note that the type-cast will succeed only, when you are prototyping the entire application. In turn, when you test a GUI component separately, no instance of your application component is created. The variable rootthis refers in such case to a generic instance of Core::Root class serving as an environment for the tested GUI component. We recommend therefore to always check the result of the type-cast operation to avoid errors during prototyping of single GUI components.

Please note that evaluating rootthis may result in null if the root object has not yet finalized the initialization code implemented in Core::Root class. Similarly, when the root object is about to be disposed, the variable rootthis is set immediately to null. Since the application component is usually the first one created and the last one destroyed, this should not be an issue. Furthermore, if your project uses simultaneously multiple application components, rootthis will refer to the recently created instance.