6 Replies Latest reply on Oct 4, 2010 1:25 PM by mumcs01

    running a method in a class from main.mxml

    mumcs01 Level 1

      So... this seems dumb, but I can't seem to run a method on an instance of a class. I should be able to

      arraycollectioninstance[0].methodinclass();    //this doesn't work

      I also tryed arraycollectioninstanace.getItemAt().methodinclass();

       

       

      All I want to do in run the function sortByUdn() that resides in the class definition from main.mxml on an instance of an arraycolleection. How do I do this and why doesn't the above attempts work? What am I missing?

       

       

      Below is my class:

      --------------------------------

      package status
      {
      import mx.collections.ArrayCollection;
      import mx.collections.IViewCursor;
      import mx.collections.Sort;
      import mx.collections.SortField;

      import org.osmf.layout.AbsoluteLayoutFacet;

      [Bindable]
      public class Rack
      {
        public var currentSort:String = 'udn';
        public var lowestUdn:int;
        public var highestUdn:int;
        public var circuits:int;
        public var rackModel:String;
        public var rackLevels:ArrayCollection = new ArrayCollection;
       
       
        public function Rack(response:XMLList):void
        {
         circuits = response[0].info.length();
         for (var i:int=0; i < circuits; i++)
         {  
          rackLevels.addItem(new Level(
           response[0].info[i].@udn,
           response[0].info[i].@circuit,
           response[0].info[i].@wsource.toString(),
           response[0].info[i].@level.toString()
           ));
         }
         findMinMax();
        }
      protected function sortByUdn():void
        {
         var udnSort:Sort = new Sort()
         udnSort.fields = new Array();
         udnSort.fields.push(new SortField('udn',true,false,true));
         this.rackLevels.sort = udnSort;
         this.rackLevels.refresh();
        }
      }
      }

        • 1. Re: running a method in a class from main.mxml
          archemedia Level 4

          If you have a funtion in your main application, you can access is by Application.application.myFunction()

          If you have a property in your main application, you can access it by Application.application.myProp

           

          So, in your case:

           

          In your main application:

           

          public var myRack:Rack

           

          You can access the methods for this intance through:

           

          Application.application.myRack.sortByUdn();

           

          Hope this helps you out

           

          Dany

          • 2. Re: running a method in a class from main.mxml
            mumcs01 Level 1

            Thanks for the response!!!

             

            So my problem is that my class (Rack) has the functions, but I'm using an arraycollection of instances of this class. So in my main Application I have:

            [

             

            public var current_rack:ArrayCollection = new ArrayCollection;

             

            then later on I'm using a current_rack.addItem(new Rack(.......)  several times to make the instances.

             

            I'm trying to call a function in one instance of current_rack[7] to perform a sort on that instance. I'm storing the sort function as part of the class, as it sorts the inards of the class instance.

             

            I really want to current_rack[0].sortByUdn();

             

            Though the above doesn't generate a compiler error, I get a runtime error of RangeError: 'Index '0' specified is out of bounds'

            [

            • 3. Re: running a method in a class from main.mxml
              JoshBeall Level 1

              That error indicates there is nothing in the ArrayCollection at index 0.  So, the problem isn't actually with calling the method, it's that the ArrayCollection has no object (which you would need to call a method on that object) at index 0.

               

              So you need to look into why there is no object at index 0.

              • 4. Re: running a method in a class from main.mxml
                mumcs01 Level 1

                My mistake, the runtime error is actually:

                ReferenceError: Error #1069: Property sortByCircuit not found on status.Rack and there is no default value.

                 

                 

                So its saying that it can't find the method in the class? But its there as the above shows?

                • 5. Re: running a method in a class from main.mxml
                  JoshBeall Level 1

                  mumcs01 wrote:

                   

                  My mistake, the runtime error is actually:

                   

                  ReferenceError: Error #1069: Property sortByCircuit not found on status.Rack and there is no default value.

                   

                   

                   

                  So its saying that it can't find the method in the class? But its there as the above shows?

                  Well, now I'm a bit confused.  I thought you were saying that the error you were getting was on this line:

                   

                  current_rack[0].sortByUdn();

                   

                  But the error message you quoted says it's having trouble finding a property (not a method) called sortByCircuit?  can you post the block of code where it's throwing the error?

                   

                  And since the error message is about a property but you say it's actually a method--are you calling it as a method?  Meaning, with parentheses after the method name?  E.g.,

                   

                  result = obj.someMethod; // This is calling a method like a property and is probably a mistake

                  result = obj.someMethod(); // This is calling a method

                  • 6. Re: running a method in a class from main.mxml
                    mumcs01 Level 1

                    Found it at last. Thanks for everyones help. My problems was the method was cast as a protected class. Changing it to Public solved it. Stupid mistake..