5 Replies Latest reply on Jun 8, 2011 1:16 PM by Nishant Jha28

    How to check if arraycollection is intialized or not

    MuskeeterAkshay

      Hi All,

          I am little new to using arraycollection in flex. I am having this particular scenario where I receive the data from server in an array. So I have to check whether the arracollection I have created is initialized or not. If it is not initialized I need to initialize it  or other wise just add array to array collection. Below is the code snippet

       

      currentTrajectory = new ArrayCollection(event.result as Array);

       

       

      var _multiTrajectoryEvent:MultiTrajectoriesEvent;

       

       

       

      _multiTrajectoryEvent=new MultiTrajectoriesEvent(MultiTrajectoriesEvent.DRAW_AUTO_TRAJ );

       

       

       

      if (!( _multiTrajectoryEvent.trajectories.source == null))

      {

       

      _multiTrajectoryEvent.trajectories.addItem(currentTrajectory);

      }

      else

      {

       

      _multiTrajectoryEvent.trajectories  = new ArrayCollection([currentTrajectory]);

      }

       

      But unfortunately the above code is not working for me since the arraycollection is not initialized first , their is no way to check me if it is initialized later or not as the compiler gives me error for checking on null value..

       

      Can any wone suggest how may I check if the array collection is initialized or not?

       

      I would appreciate any suggestions and thouhgts for the same

       

      Thanks

      Akshay

        • 1. Re: How to check if arraycollection is intialized or not
          miguel8312 Level 3

          ashay welcome.

           

          why not just trying something like this in the top of your code.

           

           

          public var arrcoll: ArrayCollection = new ArrayCollection();

           

          this should do the trick.

           

           

          if this post answers your question please mark it as such.

          Miguel

          • 2. Re: How to check if arraycollection is intialized or not
            Nishant Jha28

            Sorry to say but that is not the right approach...

             

            I am not sure abt what you are exactly doing so i will give a generic example...

             

            Say I have an ArrayCollection...

             

            private var arrC:ArrayCollection;

             

            this might be getting used internally at multiple places so you wont be sure if it has be initialized already or not. Then use a code something like this...

             

            if(arrC != null &&  arrC.length > 0)

            {

                 ...

                 ...

            }

             

            if you use

            if(arrC.length > 0)

            {

                 ...

                 ...

            }

             

            and it is not initialized it will give compile time exception...

             

            For a logic if(a && b) to be true both a and b should be true, so if even first one(a) is false it wont evaluate the second logic(b).

            So the earlier logic works.

             

            regards

            Nishant

            • 3. Re: How to check if arraycollection is intialized or not
              miguel8312 Level 3

              Nishant

              thanks for pointing that out. now my turn.

               

              <?xml version="1.0" encoding="utf-8"?>
              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                    xmlns:s="library://ns.adobe.com/flex/spark" 
                    xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                    applicationComplete="testInitializedArray()">
               <fx:Declarations>
                <!-- Place non-visual elements (e.g., services, value objects) here -->
               </fx:Declarations>
              
              <fx:Script>
               <![CDATA[
                import mx.collections.ArrayCollection;
                
                public var arrcoll:ArrayCollection  = new ArrayCollection();
                
                public function testInitializedArray():void
                {
                 
                 
                 
                 
                }
                
                
                
                public  function testNotinitilizedarray():void
                
                {
                 
                var arrcoll2:ArrayCollection; 
                 
                 
                 
                }
                
                
                
                
                
                
                
               ]]>
              </fx:Script>
              
              
              
              
              </s:Application>
              

               

              He is asking to check wether the arraycollection has been initialized or not. sure you can use your method, but if you follow my method you wont have to check because it will initialize immeditately. check for youserlf put a break at the end of function testinitialized and and inspect the variable. remenber there is always more then one to skin a cat.

              your method is just one method. you approach is not any better then my

              thanks

              Miguel

              Miguel

              1 person found this helpful
              • 4. Re: How to check if arraycollection is intialized or not
                MuskeeterAkshay Level 1

                Thanks guys... I will try out your solution and will let you know which one worked better for me!

                 

                Thanks a ton!

                 

                Akshay

                • 5. Re: How to check if arraycollection is intialized or not
                  Nishant Jha28 Level 2

                  I wasnt interested in a flame war, guess I should rephrase... i meant... sorry but that is not the right practice...

                   

                  It  will work both ways... but your approach isnt appropriate according to  oops practices as it effects performance of the application.

                   

                  A simple example to demonstrate it...

                   

                  Lets  say John has to travel 100km from hit house to his relatives. He has a  horse cart on which he can travel. His relatives have asked for 200kgs  of rice. There are 2 huge markets in his way where he will defenitely  get the rice. One is just besides his house and other is at 50km  distance. You are suggesting to buy it at the market near him, and i am  suggesting to but it at the next market. Which one is more appropriate?

                   

                  An  object should only get initialized if it is going to be used. Imagine  an app with probably 100 such collections. But a user could only use a  few of the requests. So only a few ArrayCollections will get initialized  if my approach is taken. In your case all will get initialized doesnt  matter it is required or not.

                   

                  Please dont take it as an  insult, as i had no such intentions, I was just guiding him to the right practices which mostly we dont bother about when a code is written.

                   

                  regards

                  Nishant