7 Replies Latest reply on Apr 20, 2009 3:41 PM by Michael Borbor

    How to ignore a null array collection?

    Handycam Level 1

      I need to combine the contents of 3 array collections in components in my app.  They are optional for users, so one or more may be empty.

       

      If they all have values, this works fine:

       

      var _arr:Array =[];

      var choices1:ArrayCollection = Application.application.step0.chosenList.dataProvider;

      var choices2:ArrayCollection = Application.application.step1.chosenList.dataProvider;

      var choices3:ArrayCollection = Application.application.step2.chosenList.dataProvider;

      _arr = _arr.concat(basics1,choices1.source,basics2,choices2.source,choices3.source);

       

       

      However, if any of them is null the application crashes at the concat statement.  How can I easily concatenate the ACs that have data an ignore the others, without a complex loop?

        • 1. Re: How to ignore a null array collection?
          Handycam Level 1

          I did it like this, i there a better way?

           

          var _arr:Array =[];

          var choices1:ArrayCollection = Application.application.step0.chosenList.dataProvider;

          var choices2:ArrayCollection = Application.application.step1.chosenList.dataProvider;

          var choices3:ArrayCollection = Application.application.step2.chosenList.dataProvider;

          if (choices1 != null){

          _arr= _arr.concat(basics1, choices1.source, basics2);

          } else {

          _arr= _arr.concat(basics1, basics2);

          }

          if (choices2 != null){

          _arr = _arr.concat(choices2.source);

          }

          if (choices3 != null){

          _arr = _arr.concat(choices3.source);

          }

          • 2. Re: How to ignore a null array collection?
            injpix Level 3

            To start off, I would put the concat() in a try/catch block.  So if it throws an error, it will be caught in the catch block.  Also, consider bitwise operators for situations like this.  For instance, one variable can represent what the user has selected, therefore a switch statement can be used to concatenate the array.

             

            For info on bitwise operators, I have found that this article is helpful:

             

            using bitwise operators in actionscript

            • 3. Re: How to ignore a null array collection?
              wvxvw Level 1

              1. Using Application.application is a very bad thing to do. You should use relative references.

               

              2. Comparing to null something that is not a prime (not a Number, int, Boolean etc) is redundant.

              • 4. Re: How to ignore a null array collection?
                Handycam Level 1

                Well, someone on the board suggested Application.application and it's

                actually "safer" than relative references because it will work with

                something no matter how far down the ladder the calling component is.  If I

                use parent, and move this component, it will break. So I have been told here

                several times that relative references are a bad thing so go figure :-D

                For #2 I guess you're referring to "if (choices1 != null)"? How would

                you recommend it?  Is "*if *(!choices1)" enough?  I thought that only worked

                with booleans not null values?

                • 5. Re: How to ignore a null array collection?
                  Handycam Level 1

                  Try/catch is a good idea i don't use often enough, thanks.

                  That link to the bitwise stuff gave me a headache  but thanks for that

                  too.

                  • 6. Re: How to ignore a null array collection?
                    wvxvw Level 1

                    >>

                    Well, someone on the board suggested Application.application and it's

                    actually "safer" than relative references because it will work with

                    something no matter how far down the ladder the calling component is.

                     

                    It is not "safer", it is "for lazy people" who, as a proweb says will do the same wok twice... Using Application.application is plain unwise, and, if you'll have to support that project, or, want to reuse your component some time later you'll definitelly end up rewriting it. Well, it is OK... kind of to read loaderInfo paramters from it (though, I don't see why not to grab them from where they originaly lend)... besides, writting into Application.application is just an architectual mistake.

                     

                    Yes, your guess is correct, whatever is inside if (...) clause is evaluated to Boolean by compiler, you don't have to do it's work. Even worse, the compiler isn't smart enough to recognize your redundant code, so, instead of the simple evaluation it will actually perform a comparation and evaluation.

                     

                    And... try-catch is intended for handling incorrect input in situations when it cannot be handled, thus, you have to notify the one who issued the wrong input. But if you can handle incorrect input, using try-catch is a move tone.

                    • 7. Re: How to ignore a null array collection?
                      Michael Borbor Level 4

                      I think you could do this as well

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                                          creationComplete="Alert.show(concatArrays(myArray1,myArray2,myArray3,myArray4).toString());">
                           <mx:Script>
                                <![CDATA[
                                     import mx.controls.Alert;
                      
                                     private var myArray1:Array=new Array(1,2,3,4,5);
                      
                                     private var myArray2:Array;
                      
                                     private var myArray3:Array=new Array(6,7,8,9,10);
                      
                                     private var myArray4:Array=new Array(11,12,13,14,15);
                      
                                     private function concatArrays(...myArrays):Array{
                                          var retArray:Array=new Array();
                                          for(var a:int=0;a<myArrays.length;a++){
                                               if(myArrays[a]){
                                                    retArray=retArray.concat(myArrays[a]);
                                               }
                                          }
                                          return retArray;
                                     }
                                ]]>
                           </mx:Script>     
                      </mx:Application>