1

I have a flex application with two objects: a parent widget (called an IBaseWidget) and a child widget (called a HelperWidget2). When the user clicks on a help link, a helper widget is loaded into the list of base widgets and then displayed for the user.

However, when I try to access this child widget by casting the base widget in the collection to the child widget type, the child widget returns null and I am unable to work with the widget.

The following snippet correctly returns the widget ID of the newly added widget and dispatches an event to load the widget:

var id:Number = WidgetManager.getInstance().getWidgetId("Helper");
ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, id, openQuickQueryCanvas));

Once the widget is loaded, a callback function called openQuickQueryCanvas() attempts to do another action with the helper widget:

private function openQuickQueryCanvas():void{
            var id:Number = WidgetManager.getInstance().getWidgetId("Helper");
            var bWidget:IBaseWidget = WidgetManager.getInstance().getWidget(id) as IBaseWidget;
            var helperWidget:HelperWidget2 = bWidget as HelperWidget2;
            if(helperWidget != null){
                helperWidget.quickQueryCanvas.dispatchEvent(new MouseEvent(MouseEvent.CLICK));//fire an event to open the quick query canvas
            }

        }

The problem is that helperWidget above always returns null, meaning the cast isn't successful. This doesn't make sense to me, because bWidget is of type HelperWidget2.

Any thoughts? I'm stumped...

Kyle
  • 4,202
  • 1
  • 33
  • 41

2 Answers2

2

First off, make sure that HelperWidget2 implements IBaseWidget like so

public class HelperWidget2 implements IBaseWidget

Second, I would suggest using the is keyword instead of casting and checking for null:

private function openQuickQueryCanvas():void {
                var id:Number = WidgetManager.getInstance().getWidgetId("Helper");
                var bWidget:IBaseWidget = WidgetManager.getInstance().getWidget(id) as IBaseWidget;

                if(bWidget is HelperWidget2)
                {
                  HelperWidget2(bWidget).doWhatever();
                }

            }
Jonathan Dumaine
  • 5,575
  • 3
  • 38
  • 50
  • Good thoughts - I implemented your second suggestion. I am still having trouble declaring the "HelperWidget2 implements IBaseWidget" suggestion, as HelperWidget2 is an MXML module as opposed to an actionscript class. I looked at [link](http://stackoverflow.com/questions/3187466/is-there-such-a-thing-as-an-mxml-interface) to find a way to implement an interface in an MXML component, but I am not able add the "implements" attribute to my widget (or any mx component). – Kyle Mar 02 '11 at 21:02
0

Cast the returning instance as an object, instead of HelperWidget2. You won't have intellisense for the methods at design time, but more importantly, it won't be null at run time.

var bWidget:Object = WidgetManager.getInstance().getWidget(id);
bWidget.doWhatever();
rboone
  • 521
  • 5
  • 10