7 Replies Latest reply on Jul 13, 2010 9:23 AM by rtalton

    Set Array

    lkenney2

      This question is escaping me for some reason. I have some data that needs to stored in an array or a class. I say this, because the object needs to be indexed, and I need to be able to set the individual properties of the object. So it needs to word like this:

       

      myObj[i].score = score;

      myObj[i].word = word;

       

      etc.

       

      This is necessary because multiple different functions get the various values collected for this object.I can't just do a general push (myObj.push = ({score:score, word:word});), or a multidimesional array (myObj[i] = [score, word]).

       

      Can anyone offer me some help? Thanks!

        • 1. Re: Set Array
          rootsounds Level 4

          Create a custom class with whatever properties you need and store objects of that class in the Array. You shouldn't have to cast on retrieval, but it's a good idea since it makes the code clearer. You could obscure the casting if you instead use a custom ArrayCollection and override the getItemAt() method to return the specific custom class type.

          • 2. Re: Set Array
            lkenney2 Level 1

            Hey rootsounds,

             

            Do you think you could provide an example of that? My English is okay, and it would help if you gave an exmaple.

             

            Thanks.

            • 3. Re: Set Array
              rootsounds Level 4

               

              public class MyArrayCollection extends ArrayCollection

               

              {

               

                   override public function getItemAt(index:int, prefetch:int = 0):MyCustomDataClass
                   {
                        return MyCustomDataClass( super.getItemAt(index, prefetch) );
                   }

              }

              Note that this doesn't have any type-checking, so it's assumed that you're not going to be throwing anything other than MyCustomDataClass into the ArrayCollection.

              • 4. Re: Set Array
                rootsounds Level 4

                Is this what you are after?


                • 5. Re: Set Array
                  lkenney2 Level 1

                  Hi rootsound,

                   

                  That gives me an error for 1023: Incompatible override adn 1144: Interface method getItemAt in namespace mx.collections:IList is implemented with an incompatiable signiture in class MyArrayCollection.

                  • 6. Re: Set Array
                    rootsounds Level 4

                    Sorry. Forgot about that. We can't break the interface contract. However, we can write a new method with the same body.

                    • 7. Re: Set Array
                      rtalton Level 4

                      You might consider your own strongly-typed value object. Within your application, you have a method you call to create a new value object whenever you need it. You could then store multiple instances of these value objects within an ArrayCollection which allows soring, filtering and searching them.

                       

                      package vo
                      {
                          [Bindable]
                          public class testResult
                          {
                              public var score:int=0;
                              public var word:String="";
                             
                              public function testResult(obj:Object = null)
                              {
                                  if (obj != null)
                                  {
                                  this.score = obj.score;
                                  this.word = obj.word;              
                                  }
                                 
                              }

                       

                          }
                      }