7 Replies Latest reply on Jun 22, 2009 7:08 PM by john__p

    Actionscript variable name function call

    john__p Level 1

      I'm attempting to call a function dynamically using a string variable.  I know that to normally do this, I would use code like the following:

       

           var functionName:String = "ii_" + currentItem.id;

           var myData:Array = this[functionName](parameter1, parameter2, parameter3);

       

      However, my function is actually in another flex directory/package called UploadApps.customUploadFunctions.  I've tried this sort of thing:

       

           var functionName:String = "ii_" + currentItem.id;

           var myData:Array = UploadApps.customUploadFunctions.this[functionName](parameter1, parameter2, parameter3);

       

      but this doesn't work.  Any ideas on how I go about calling a function with a variable name in another package?

       

      John

        • 1. Re: Actionscript variable name function call
          Flex harUI Adobe Employee

          Depends on what how the functions in the package are defined, but

               UploadApps.customUploadFunctions[functionName]....

           

          should work

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: Actionscript variable name function call
            john__p Level 1

            Thanks Alex.  The function in the package is defined as:

             

                     package UploadApps.customUploadApps
                      {
                          :

                          :

                          :

                          :

                          public function ii_000010(inputItem:InputItem, periodDates:Object, inputItemBox:VBox, branchId:int, uploadStatusData:ArrayCollection, validatorArray:Array):Array {

                           :

                           :

             

            with the file name being ii_000010.as, but when I call it as such:

             

                     import UploadApps.customUploadApps.*;

                           :

                           :

                           :

                      var functionName:String = "ii_" + currentItem.id;

                      someArray = UploadApps.customUploadApps[functionName](currentItem, thisFieldDates, inputItemBox, BranchId, uploadStatusData, validatorArray);

             

            I get the errors:

             

                     1120: Access of undefined property UploadApps.customUploadApps.

                      1182: Package cannot be used as a value: UploadApps.customUploadApps.

             

            John

            • 3. Re: Actionscript variable name function call
              Flex harUI Adobe Employee

              Packages aren't objects in AS3 (they were in AS2).  Thus, I can't think of a way to get an object to use a [] lookup on, although I might just be missing something.

               

              I would use ApplicationDomain.getDefinition() or flash.utils.getDefinitionByName() instead.

               

                            var functionName:String = "ii_" + currentItem.id;

                        var function:Function = getDefinitionByName("UploadApps.customUploadApps." + functionName);

                             someArray = function(currentItem, thisFieldDates, inputItemBox, BranchId, uploadStatusData, validatorArray);

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              1 person found this helpful
              • 4. Re: Actionscript variable name function call
                john__p Level 1

                Alex,  I gave the getDefinitionByName method a try and it successfully compiled.  However when I went to run it, I got the following error:

                 

                     Error #1065: Variable ii_000010 is not defined.

                 

                where ii_000010 is the name of my custom function.  Following some further investigation, it appears that my package/function is not actually compiled in to the swf at compile time.  Apparently the compiler only includes functions that are actually used, even if they were imported?  Of course my function technically isn't being used as it is being referred to by a string variable.

                 

                From what I read the only way around it is to declare a variable of type function and assign the custom function to it before calling getDefinitionByName.  So for example in my case this would be:

                 

                     var functionName:String = "ii_" + currentItem.id;

                     var dummyFunction:Function = UploadApps.customUploadApps.ii_000010;

                     var customFunction:Object = getDefinitionByName("UploadApps.customUploadApps."+functionName);

                 

                So I will have to include a line like the dummyFunction one above in my code for each function I wish to use; kind of defeats the purpose of having the dynamically named function really.

                 

                Does my hypothesis sound right?  Any other ideas that I could try?

                 

                John

                • 5. Re: Actionscript variable name function call
                  john__p Level 1

                  Has anyone else come up against this problem?

                   

                  John

                  • 6. Re: Actionscript variable name function call
                    GordonSmith Level 4

                    My suggestion is to stop using package-level methods. Just make them static methods in a utility class like we do in the framework, in the mx.utils package. Then you can easily call them as UtilityClassName[functionName](param1, param2, param3). Since programming in AS3 is 99.9% about classes, I don't know why AS3 even allows package-level methods.

                     

                    Gordon Smith

                    Adobe Flex SDK Team

                    • 7. Re: Actionscript variable name function call
                      john__p Level 1

                      Thanks Gordon.  Setting up a utility class as per your suggestion sorted things out.

                       

                      John