12 Replies Latest reply on May 14, 2009 12:42 PM by Miggl

    Check if Function Variable is Instanciated

    Miggl Level 1

      How can I check if a function variable is instanciated? I have the following in a custom component:

       

      [Bindable]
      public var selectionFunction:Function;
      

       

      I would now like to check at runtime if anything has been assigned to the selectionFunction variable, i.e. if it has been instanciated with a function. The following methods have failed me so far:

      if (this.selectionFunction is Function)     //failure, type error #1009
      
      if (this["selectionFunction"] is Function)  //failure, type error #1009
      
      if (this.selectionFunction != null)         //failure, type error #1009
      
      if ("selectionFunction" in this)            //failure, type error #1009
      

       

      Any ideas on how to achieve this at run-time?

       

      Thanks!

      -Mike

        • 1. Re: Check if Function Variable is Instanciated
          ATIF FAROOQ Level 3

          hi,

             you can check by this method

           

           

           

          trace(String(selectionFunction)=="null")
          • 2. Re: Check if Function Variable is Instanciated
            Miggl Level 1

            Thanks for the suggestion, but that also resulted in a type error #1009.

            • 3. Re: Check if Function Variable is Instanciated
              _Natasha_ Level 4

              Hi Mike,

              Error description.

              1009Cannot access a property or method of a null object reference.

              So it seems that you this is null.

               

              In sdk source functions always checks != null.

              1 person found this helpful
              • 4. Re: Check if Function Variable is Instanciated
                Michael Borbor Level 4

                Try if

                 

                Sincerely,

                 

                Michael

                1 person found this helpful
                • 5. Re: Check if Function Variable is Instanciated
                  Michael Borbor Level 4

                  Try if(selectionFormula!=null)

                   

                  Sincerely,

                   

                  Michael

                   

                  El 14/05/2009, a las 13:56, Miggl <forums@adobe.com> escribió:

                   

                  >

                  How can I check if a function variable is instanciated? I have the 

                  following in a custom component:

                  >

                  public var selectionFunction:Function;

                  >

                  >

                  I would now like to check at runtime if anything has been assigned 

                  to the selectionFunction variable, i.e. if it has been instanciated 

                  with a function. The following methods have failed me so far:

                  if (this.selectionFunction is Function)     //failure, type error 

                  #1009

                  >

                  if (this["selectionFunction"] is Function)  //failure, type error 

                  #1009

                  >

                  if (this.selectionFunction != null)         //failure, type error 

                  #1009

                  >

                  if ("selectionFunction" in this)            //failure, type error 

                  #1009

                  >

                  >

                  Any ideas on how to achieve this at run-time?

                  >

                  Thanks

                  -Mike

                  >

                  • 6. Re: Check if Function Variable is Instanciated
                    Miggl Level 1

                    Perhaps, but I need to be able to make that determination without triggering an error. Triggering an error situation to check properties, objects, or whatever is unacceptable from a programming point of view.

                     

                    Thanks,

                    Mike

                    • 7. Re: Check if Function Variable is Instanciated
                      ATIF FAROOQ Level 3

                      then problem is not here . You may be doing some wrong assignments some where else in your code.

                      • 8. Re: Check if Function Variable is Instanciated
                        Miggl Level 1

                        Hi Michael,

                         

                        That was one of my original test-scenarios I tried. Also results in an 1009 error.

                         

                        Thanks,

                        Mike

                        • 9. Re: Check if Function Variable is Instanciated
                          Miggl Level 1

                          Try this on for size:

                          <?xml version="1.0" encoding="utf-8"?>
                          <mx:Application
                               xmlns:mx="http://www.adobe.com/2006/mxml"
                               layout="absolute"
                               creationComplete="initApp()"
                          >
                               <mx:Script>
                                    <![CDATA[
                                         import mx.controls.Alert;
                                         
                                         private var myFunction     :Function;
                                         
                                         private function initApp():void
                                         {
                                              try
                                              {
                                                   Alert.show("Start");
                                                   if (this.myFunction != null)
                                                   {
                                                        Alert.show("Function Exists!");
                                                   }
                                                   else
                                                   {
                                                        Alert.show("Function Does Not Exist!");
                                                   }
                                                   Alert.show("End");
                                              }
                                              catch (error:Error)
                                              {
                                                   Alert.show("Name: " + error.name + "\nID: " + error.errorID + "\nMessage: " + error.message + "\nStack Trace: " + error.getStackTrace() + "\nError: " + error.toString(), "Failure");
                                              }
                                         }
                                    ]]>
                               </mx:Script>
                          </mx:Application>
                          

                           

                          This example would show that is does work. I'll keep looking.

                           

                          Thanks!

                          Mike

                          • 10. Re: Check if Function Variable is Instanciated
                            M. Regert Level 1

                            function != null seems to work for me:

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                             

                            <?xml version="1.0" encoding="utf-8"?>

                            <mx:Application

                             

                             

                            xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

                            >

                             

                             

                            <mx:Script>

                            <![CDATA[

                             

                             

                            import

                            mx.controls.Alert;

                            [

                             

                            Bindable

                            ]

                             

                             

                            public var

                            selectionFunction:Function;

                             

                             

                             

                            private function check():void

                            {

                             

                             

                            if (selectionFunction != null

                            ) {

                            Alert.show(

                             

                            "Function is defined", "Success"

                            );

                            }

                             

                            else

                            {

                            Alert.show(

                             

                            "Function is NOT defined", "Failure"

                            );

                            }

                            }

                             

                             

                             

                            private function foo():void

                            {

                             

                             

                            trace("some dummy function"

                            );

                            }

                             

                             

                             

                            private function onToggle(event:Event):void

                            {

                             

                             

                            var toggle:CheckBox = event.currentTarget as

                            CheckBox;

                             

                             

                            if

                            (toggle.selected) {

                            selectionFunction = foo;

                            }

                             

                            else

                            {

                            selectionFunction =

                             

                            null

                            ;

                            }

                            check();

                            }

                            ]]>

                             

                             

                            </mx:Script>

                             

                             

                            <mx:Panel width="400" height="300" layout="absolute"

                            >

                             

                             

                            <mx:CheckBox id="cbToggle" label="Assign value to function variable" change="onToggle(event)" horizontalCenter="0" verticalCenter="0"

                            />

                             

                             

                            </mx:Panel>

                            • 11. Re: Check if Function Variable is Instanciated
                              ATIF FAROOQ Level 3

                              i just executed your code it works fine as it is . it never goes to catch statement.

                              • 12. Re: Check if Function Variable is Instanciated
                                Miggl Level 1

                                Atif, you're absolutely right.

                                 

                                Thanks to everyone for bearing with me. It took my a while to realize what Atif said (that it must be something else in my code). In fact, another if statement had exactly the same issue, and was erroring out there. After rectifying that, everything worked as expected.

                                 

                                Thanks again! I doled out what what experience points I could (wish I could have given everyone helpful on this, but there were only 2).

                                 

                                -Mike