1 Reply Latest reply on May 14, 2009 3:52 AM by Barna Biro

    Array.prototype problem

    christoferek

      Hi All,

       

      What is wrong with this code that I get the error shown below?

       

       

              Array.prototype.pushUnique = function( a:Array ): void {
                  var l:Number = this.length;
                  var exists:Boolean = false;
                 
                  for( var i:int=0; i<l; i++ ){
                      if( this[i][0] == a[0] ) {
                          exists = true;
                      }
                  }
                  if( !exists ) {
                      this.push( a );
                  }
              }

       

      I call it like this:

       

           allBlobsArray.pushUnique(tempArr);

       

      Finally I get an error:

       

      TypeError: Error #1034: Type Coercion failed: cannot convert Function-2499 to flash.events.InvokeEvent.
          at mx.core::WindowedApplication/dispatchPendingInvokes()[E:\dev\3.0.x\frameworks\projects\ai rframework\src\mx\core\WindowedApplication.as:2539]
          at mx.core::WindowedApplication/enterFrameHandler()[E:\dev\3.0.x\frameworks\projects\airfram ework\src\mx\core\WindowedApplication.as:2526]

       

      More over:

              Array.prototype.pushUnique = function( a:Array ): void {
      //            var l:Number = this.length;
      //            var exists:Boolean = false;
      //           
      //            for( var i:int=0; i<l; i++ ){
      //                //Debug.warning("in GetConfigData");
      //                //$temp_arr:Array = this[i];
      //                if( this[i][0] == a[0] ) {
      //                    exists = true;
      //                }
      //            }
      //            if( !exists ) {
      //                Debug.warning("pushDone tabLenght="+l);
      //                Debug.warning(a);
                      this.push( a );
      //            }
              }

       

      Gives me the same error.

       

      Regards

       

      Christoferek

        • 1. Re: Array.prototype problem
          Barna Biro Level 3

          Hi again,

           

          I have to say, the way you are doing the checking is off the hood... Did you write that on your own or did you find some bad resources somewhere? I think you're the first guy I have seen in quite a while to use prototype in AS 3.0; That method is really not cool. Why don't you simply do something like this:

           

          package 
          {
              import flash.display.Sprite;
          
              public class ArrayExample extends Sprite
              {
                  private var _arr:Array = new Array();
                  
                  public function ArrayExample()
                  {
                      // Add some items to the list.
                      trace("Added to the list: " + pushUnique("a", _arr));
                      trace("Added to the list: " + pushUnique("b", _arr));
                      trace("Added to the list: " + pushUnique("c", _arr));
                      trace("Added to the list: " + pushUnique("a", _arr));
                      trace("Added to the list: " + pushUnique("a", _arr));
                      trace("Added to the list: " + pushUnique("b", _arr));
                      trace("Added to the list: " + pushUnique("d", _arr));
                      
                      // Display the final list content.
                      trace("Final list values: " + _arr.valueOf());
                  }
                  
                  /**
                  * Check if the passed <code>Object</code> exists in the passed <code>Array</code>.
                  * 
                  * @param value The value that you want to check to the <code>Array</code>.
                  * @param arr The list of values.
                  * 
                  * @ return Returns <code>false</code> if the object alread exists in the <code>Array</code>; else,
                  * it pushes the new value into the <code>Array</code> and returns <code>true</code> to signal the success.
                  */
                  public function pushUnique(value:Object, arr:Array):Boolean
                  {
                      // If the Array if empty then add the item and return true.
                      if (arr.length == 0)
                      {
                          arr.push(value);
                          return true;
                      }
                      
                      // Check if the value already exists in the list.
                      var timesFound:int = 0;
                      for each (var item:Object in arr)
                          if (item == value)
                              timesFound++;
                              
                      // If it hasn't found then add it to the list; else, return false.
                      if (timesFound == 0)
                      {
                          arr.push(value);
                          return true;
                      }
                      else
                      {
                          return false;
                      }
                  }
              }
          }
          

           

          Even better, you could create a utility class that implements the pushUnique method in the same fassion and reuse it in all your projects. You could also take some of the logic out, maybe only return "true" or "false" if the object exists and decide somewhere else if you want to add it to the list or do something else with the received information. Hope it helps.

           

          With best regards,

          Barna Biro

          Blog: http://blog.wisebisoft.com