4 Replies Latest reply on Jul 14, 2010 3:20 PM by Rothrock

    Arrays within custom Classes - same array for different instances?

    DTshults

      Hello all,

       

      I have made a very simple custom class for keeping track of groups of offices for a company.  The class has a Number variable to tell it how many different offices there are, and an Array to store the individual offices by name.  It looks like this.

       

      class officeCluster
      {
          static var _className:String = "officeCluster";
         
          // variables
          var numOffices:Number;
          var locationArray:Array = new Array();
         
          // functions
          function officeCluster()
          {
              trace("officeCluster constructor");
          }
      }

       

      Very simple!

       

      Now, it is my understand that when I create different instances of the class, they will each have their own version of "numOffices" and their own version of "locationArray".

       

      When I run traces of "numOffices", this seems to be true.  For example,

       

      trace(manufacturingOfficeCluster.numOffices);
      trace(servicesOfficeCluster.numOffices);

       

      yields

       

      5

      4

       

      In the output panel, which is correct.  However, there is trouble with the locationArray.  It seems that as I assign different values to it, regardless of what instance I specify, there is only ONE array- NOT one for each instance.

       

      In other words,

       

      trace(manufacturingOfficeCluster.locationArray[1].theLocation);   // theLocation is a String.  The locationArray itself holds Objects.
      trace(servicesOfficeCluster.locationArray[1].theLocation);

       

      yields

       

      New Haven, CT
      New Haven, CT

       

      even though I have defined elsewhere that they are different!

       

      Is anyone aware of any issues partaining to using Arrays within Class instances?  Any help would be appreciated!

       

      note:  I've been able to work around this by creating multiple arrays within the class and using a different one for each instance, but this seems very sloppy.

        • 1. Re: Arrays within custom Classes - same array for different instances?
          kglad Adobe Community Professional & MVP

          each array is unique to each class instance.

           

          you have some other error which you can confirm by assigning both arrays a different element and checking:

           


           

          manufacturingOfficeCluster.locationArray[1].theLocation = "l1";
          servicesOfficeCluster.locationArray[1].theLocation = "l2";

           

          trace(manufacturingOfficeCluster.locationArray[1].theLocation);

          trace(servicesOfficeCluster.locationArray[1].theLocation);

           


          1 person found this helpful
          • 2. Re: Arrays within custom Classes - same array for different instances?
            DTshults Level 1

            Unfortunately, the code segment you attached results in:

             

            12

            12

             

            in the output panel.   So the problem must lie elsewhere!  Let me give some more detail...

             

            There are several files involved. The "officeCluster" class file looks like this:

             

            class officeCluster
            {
                 static var _className:String = "officeCluster";
                 
                 // variables
                 var numOffices:Number;
                 
                 var locationArray:Array = new Array();
                      
                 // functions
                 function officeCluster() 
                 {
                    trace("officeCluster constructor");
                }
            }
            

             

            I have two actionscript files which contain object data for the individual offices.  They look like this...

             

            var servicesOfficeCluster = new officeCluster();
            servicesOfficeCluster.numOffices = 4;
            
            var newHope:Object = new Object();
            newHope.locationName = "New Hope Office";
            newHope.theLocation = "New Hope, NJ";
            //more data
            servicesOfficeCluster.locationArray[0] = newHope; //array index is incremented with each entry
            
            //more Objects...
            

             

            and like this...

             

            var manufacturingOfficeCluster = new officeCluster();
            manufacturingOfficeCluster.numOffices = 5;
            
            var hartford:Object = new Object();
            hartford.locationName = "Hartford Office";
            hartford.theLocation = "Hartford, CT";
            //more data
            manufacturingOfficeCluster.locationArray[0] = hartford; //array index is incremented with each entry
            
            //more Objects...
            

             

            As you can see, the only difference is the name of the officeCluster instance, and of course the Object data itself.  Finally, these are all used by the main file, which looks like this- I have commented out all the code except for our little test -

             

            import officeCluster;
            
            #include "manufacturingList.as"
            #include "servicesList.as"
            
            /*lots of commented code*/
            
            manufacturingOfficeCluster.locationArray[1].theLocation = "l1";
            servicesOfficeCluster.locationArray[1].theLocation = "l2";
             
            
            trace(manufacturingOfficeCluster.locationArray[1].theLocation);
            trace(servicesOfficeCluster.locationArray[1].theLocation);
            

             

            Which, unfortunately, still yields

             

            12

            12

             

            as output :\  Any ideas?  Is there something wrong with the way I have set up the class file?  Something wrong in the two AS files?  I'm really starting to bang my head against the wall with this one.

             

            Thanks

            • 3. Re: Arrays within custom Classes - same array for different instances?
              kglad Adobe Community Professional & MVP

              yes, there's a problem with the way you're using arrays.  the basic problem is this:

               

              var a1:Array = [0];

              var a2:Array = [1];

               

              a1 = a2;

              trace(a1[0]);  // should be 1

               

              a2 = [2];

               

              trace(a1[0]);  // you might think would still be zero, but it's not.  it will be 2

               

              to remedy (and clean up your coding style), use a getter/setter in your officeCluster class to deal with locationArray.

              • 4. Re: Arrays within custom Classes - same array for different instances?
                Rothrock Level 5

                I would rewrite the class like this. That will take care of the issue.

                 

                class OfficeCluster
                {
                    static var _className:String = "OfficeCluster";
                   
                    // variables
                    var numOffices:Number;
                    var locationArray:Array;
                   
                    // functions
                    function OfficeCluster()
                    {
                        trace("officeCluster constructor");

                        locationArray=[];
                    }
                }

                 

                Also class names should start with capital letters and the instances with lower.

                 

                Also you might want to formalize your data structures and instead of using a generic Object type make a class for that.