994 views
in Embedded Wizard Studio by

Hallo,

i want to create a methode to deleted different screens on my application. The screens, which i want to delete are different types of classes. In my methode i do this:

// Get the Root
var Core::Root rootObject = GetRoot();

// if a View is active, close it
var Core::View view = rootObject.FindNextView( null, Core::ViewState[ Visible ] );
while ( view != null )
{
 // find next view on the screen
 view = rootObject.FindNextView( view, Core::ViewState[ Visible ] );
 
 // try to cast the screen
 var MyApp::Screen1 removeScreen = (MyApp::Screen1)view;
 
 // cast has worked, remove the screen
 if( removeScreen != null )
  rootObject.Remove( removeScreen );
}

My goal is to pass MyApp::Screen1 as an argument to this class, but also other types of classes like MyApp::Screen3 and so on. I don't know how to set the argument for this in the methode. Is there something like any? Or a other simple way to do this in embedded wizard? Or do i have to create for each class a own methode?

Thank you and best regards

Torben

1 Answer

0 votes
by

Hello,

similarly to other object-oriented programming languages you have to specify the type of the argument to be a class, which is common ancestor of all classes you intend to pass in the argument. Following are the possibilities:

1. Assuming, your classes MyApp::Screen1, MyApp::Screen3, etc. are typical GUI components, then these descend from the Mosaic class Core::Group. Thus you can specify the type of the argument to be Core::Group. Accordingly, the method will accept any GUI component instance being passed in this parameter, not only Screen1 or Screen3.

2. If you want to limit the method to accept only the particular screen components, use an additional class as a common intermediate ancestor for all the screens. In practice: Create a new empty GUI component and name it e.g. MyApp::ScreenCommon. Leave the component unchanged. Then change the attribute SuperClass of your already existing component Screen1, Screen3, etc. to refer to the just created MyApp::ScreenCommon component. With this, your existing screen components become descendends of the MyApp::ScreenCommon component. Finally use the type MyApp::ScreenCommon in the declaration of the method argument. Such method will accept only instances of classes descending from MyApp::ScreenCommon.

3. Declare the argument with the very common type object. Such method will accept any object regardless of it is a screen, a timer, a view, handler, device driver instance, etc.

Which approach you select it's up to you. Please consider, within the method you can per default access properties, methods, etc. of the passed instance according to the specified argument type. If you have a new property added to the class MyApp::Screen3 and the method is declared with Core::Group as type for the argument, so trying to access this new property in context of the argument will report a Chora compiler error, since the property is unknown in Core::Group class. In such case you have to apply the object runtime cast operator on the argument to verity at the runtime, that the passed instance is really an instance of the particular class. If the cast is successful, you are able to access the particular property:

// Try to cast the argument
var MyApp::Screen1 screen1 = (MyApp::Screen1)method_argument;

// Successful? Then access the particular property
if ( screen1 != null )
  screen1.SomeProperty = ...

By the way, in your above implementation is an error. The first two statements within the while-loop should be exchanged, otherwise the first found screen is never removed:

while ( view != null )
{
  // try to cast the screen
  var MyApp::Screen1 removeScreen = (MyApp::Screen1)view;

  // find next view on the screen
  view = rootObject.FindNextView( view, Core::ViewState[ Visible ] );
 
  // cast has worked, remove the screen
  if( removeScreen != null )
    rootObject.Remove( removeScreen );
}

Hope it helps you.

Best regards

Paul

by
Thank you Paul for the detailed anwser! That help's me.

Best regards

Torben

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

...