10 Replies Latest reply on May 4, 2010 1:48 PM by GordonSmith

    How to Pass the Name of Array to Another Function?

    alice_data Level 1

      Hi,

       

        I have a function call as shown here:  c.draw(colorRead.population);

       

      In this function, I would like it to be able to print out population as part of the string for my toolTip. Note, what is in the parameter here may not be the case in every of the instance I need to create.

       

      public function draw(a:Array):void{            
                            
                   var j:int=0;         
                   for (var s:String in regions) {  
                                             
                       trace("\n" + s);                
                       shape = new UIComponent();  
                       shape.toolTip = s; //Insert in toolTip     
                       gr = shape.graphics;
                       gr.lineStyle(3);//Define line style
                       coords = regions[s];    
                       gr.moveTo(coords[0],coords[1]);
                                       
                   for (var i:int = 2; i < coords.length; i += 2) gr.lineTo(coords[i],coords[i + 1]);                                 
                       gr.lineTo(coords[0],coords[1]);    //return the code back to the beginning                 
                       canvas.addChild(shape);             
                   }
                 }           

       

      How can I just pass in the parameters here without making the toolTip print out all the contents in the array?

      Thanks for your help.

       

      Alice

        • 1. Re: How to Pass the Name of Array to Another Function?
          alice_data Level 1

          Well, I did a work around, so I could get what I want. This is what I did:

           

          I added another param/variable to pass on to the draw function as string, so the function call becomes this: c.draw(colorRead.population,"Population");  

          I then could have the for loop print out the variable for the string that I declared.

           

          public function draw(a:Array,b:String):void{            
                                    
                       var j:int=0;                     
                       for (var s:String in regions) {           
                       
                           shape = new UIComponent();  
                           shape.toolTip = s + "\t" + b;//Insert in toolTip    
                           gr = shape.graphics;
                           gr.lineStyle(3);//Define line style 
                       
                           coords = regions[s]; 
                           var randomNumber:uint = Math.floor(Math.random()* (0x1 + 0xFF0000 - 0x00FF00)) + 0x00FF00;           
                           gr.beginFill(randomNumber);        
                           gr.moveTo(coords[0],coords[1]);
                                           
                       for (var i:int = 2; i < coords.length; i += 2) gr.lineTo(coords[i],coords[i + 1]);                       
                           gr.lineTo(coords[0],coords[1]);    //return the code back to the beginning   
                           gr.endFill(); //Put this in if there is a color
                           canvas.addChild(shape);      
                       }      
                     }

           

           

          Still, I hope that this is not the only method. Is there an alternative I could use?

           

          Thanks for your help.

           

          Alice

          • 2. Re: How to Pass the Name of Array to Another Function?
            Ankur Arora Level 1

            does that means you need to pass a name of array as string and then use this to find out all items in that particular array? if that so what you can do is something like this:-

             

             

            var myArrName:Array = new Array();
            myArrName.push("ANKUR");
            myArrName.push("ANKUR2");
            
            function getArr(strArrName:String):void
            {
                 var arr:Array = new Array(this[strArrName]);
                 trace(arr)
            }
            
            getArr("myArrName");
            

             

             

            let me know if you are looking for something else.

             

             

             


            Ankur Arora

            Project Leader(Flash and Flex)

            http://flashdeveloper.blogspot.com

            http://www.ankur-arora.com


            • 3. Re: How to Pass the Name of Array to Another Function?
              alice_data Level 1

              Yes, I am looking for the name of the array that is passed in the functon. However, I am not using the information to find out the contents of that array, sine my intension is to find that out so I can put that in the "trace" statement to find out what category array it is.

               

              Your example looks similar to what I want, but I can't use it because: when I pass in the information, the array already has "contents".

               

              Does this clarifcation help?

               

              Thanks.

               

              Alice

              • 4. Re: How to Pass the Name of Array to Another Function?
                pauland Level 4

                Alice (or is it John?),

                 

                I'm not sure why you are trying to use array names in tooltips and trying to find out the name of the supplied array, but I really think it's not the right approach at all.

                 

                If you are just debugging, then trace or use the debugger to look at the call stack before the call.

                 

                If you are naming arrays as a means of labeling them to pass information to the user that not a good way to proceed. If you have information to pass to the user, pass it explicitly via storage rather than attempt to use variable names as a vehicle for passing information.

                 

                Inside a function, the names of the variables used to populate the parameter list should not be of any interest at all.

                 

                Paul

                1 person found this helpful
                • 5. Re: How to Pass the Name of Array to Another Function?
                  alice_data Level 1

                  Hi,

                   

                  I am not sure if this is the right approach, either. I am using this method not just for debugging purposes either. You see, I have a couple of called instances of uiComponents that I would like to call on one single page. Yet, since the instances would only look different in color and not by the shape, I need something to be able to distinguish them apart for users.

                   

                    I thought of using tooltips, by telling users what is the name of uiComponent, and possibly what type of content is on there. The type of content, for the time being, is named by the different arrays that I am calling.

                   

                    The example from my original post works, but I am wondering if it possible that I don't have to import two parameters, which is the actual array, and the name of the array, in string format.

                   

                  I hope this makes sense what I am going for,

                   

                  Thanks.

                   

                  Alice

                  • 6. Re: How to Pass the Name of Array to Another Function?
                    pauland Level 4

                    I'm none the wiser what you are doing.

                    • 7. Re: How to Pass the Name of Array to Another Function?
                      alice_data Level 1

                      Oh, so there is no other alternative?

                      Thanks for the information.

                       

                      Alice

                      • 8. Re: How to Pass the Name of Array to Another Function?
                        Ankur Arora Level 1

                        I think your way of doing it is suited to your application although this is not a good approach but I would suggest you need not change that because that might require a significant change in your current application.

                        • 9. Re: How to Pass the Name of Array to Another Function?
                          alice_data Level 1

                          Yeah, I realize that,

                           

                          I am curious, maybe I won't change it, but this could be used for future reference. What are the possible things I have to

                          do to accomplish that?

                           

                          Thanks for your help.

                           

                          Alice

                          • 10. Re: How to Pass the Name of Array to Another Function?
                            GordonSmith Level 4

                            You are confusing objects with variable that refer to objects. An Array object doesn't actually have a name. In the expression colorRead.population, "population" is the name of a variable in the colorRead object. You can set this variable to refer to an Array object, but you could set other variables to refer to the same Array object, producing multiple variable names for the same object. You cannot tell in ActionScript how many variables are referring to any particular object, or what their names are.

                             

                            Gordon Smith

                            Adobe Flex SDK Team