805 views
in Getting started by

Hi,

As suggested in this answer, I've disposed a container (static) in my application from which I create dynamically the MainScreen component.

var Application::ScreenMain ScreenMain = new Application::ScreenMain;
Container.PresentDialog(ScreenMain,null, null, null, null, null, null, null, null, false );

In the MainScreen component, I'd like however to access the Application::Application object to retrieve the value of a variable but the Owner object is null, even the first layer that should be the container. Here's what I'm simply trying to do in the Init method() :

var Application::Application app = (Application::Application)Owner.Owner;

My first guess was that it didn't work because the object was not fully initialized yet. But no, it's not that, the Owner object is still null when accessed from another method after the init is completed.

I'm confused because I'm able to access the application object from another object that was created similarly (child of the MainScreen) but not here.

What am I missing ?

Also, I don't understand the difference between parentthis and Owner.

Thanks in advance for your explanations.

 

 

 

 

1 Answer

+2 votes
by
 
Best answer

Hello,

let me first explain the difference between Owner and parentthis. There are two possibilities to create an object (an instance of a class):

Option 1: dynamically by using the new operator. The resulting object exists detached from other objects. In this case parentthis is null.

Option2: you embedd the object inside another class. The object will be nested inside this class. At the runtime when you create an object of this class, all nested objects are also instantiated and all of them are a fixed part of the superior object. In this case parentthis of the nested obejct refers to its superior object.

In other words, when you have several objects nested at the design time one inside another, with parentthis the nested object can access its superior object. This aggregation is established at the design time and can be changed anymore. This is valid for all kinds of objects (not necesarily GUI component). Accordingly a Timer object embedded within a class component will refer with its own parentthis to this component.

Owner, in turn, establishes a relation between two GUI component (it is available in objects acting as GUI component only). The both components have not necessarily be embedded one inside another. Unlike parentthis the relation can also change at the runtime. To establish the relation you use the method Add() to disolve it you use Remove().

Now let me address your actual problem. You create dynamically (Option 1 above) an instance of Application::ScreenMain and then you present it within the Container component. This is correct approach. During initialization time of the ScreenMain you would like to access the Application component (root object). This fails. The reason is: at this time, the just created instance of ScreenMain is not part of the view tree. It does not know the root object. Calling its GetRoot() returns null.

Solution: in your ScreenMain move the initialization code to a separate method. Declare the parameters of the method with all the vallues you want to pass to the ScreenMain or with the class of your Application component. In this manner the ScreenMain can perform its initialization. Call the method explicitly just in the moment when you create the instance of the  ScreenMain class. For example:

var Application::ScreenMain ScreenMain = new Application::ScreenMain;

ScreenMain.Initialize( value1, value2, value3, ..., GetRoot());

Container.PresentDialog(ScreenMain,null, null, null, null, null, null, null, null, false );

Instead the method Initialize() you can also implement in ScreenMain diverse properties to setup this component.

Does it help you?

Best regards

Paul Banach

 

by
Totally. Don't know what I'd do without your invaluable help ! A huge thanks for your support again.

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...