4 Replies Latest reply on Sep 13, 2011 8:15 AM by kokorito

    static class error

    Jerry62712 Level 1

      I have a static class:

      package VO
      {
          public class ArrayValues
          {
              private static var instance:ArrayValues = new ArrayValues();

              // dummy values will be first replaced with English versions
              // if the user clicks "Spanish" they will be replaced with that
              private var yesNoArray:Array = new Array('1','2');
              private var utilityArray:Array = new Array('1','2');

              public function ArrayValues()
              {
                  throw Error("ArrayValues is called by its instance");
              }

              public static function getInstance():ArrayValues
              {
                  return instance;
              }

              public function getYesNoArray():Array
              {
                  return yesNoArray;
              }
              public function setYesNoArray(input:Array):void
              {
                  yesNoArray = input;
              }

      It is invoked here:

              import VO.ArrayValues;
              import VO.InputData;
              import VO.InputLabels;

              [Bindable]
              public var saveData:InputData = InputData.getInstance();

              [Bindable]
      168       public var getArrays:ArrayValues = ArrayValues.getInstance();
      ...

       

      It produces this error:

      Error: ArrayValues is called by its instance
          at VO::ArrayValues()[C:\FSCalc\Flex\FSCalc\src\VO\ArrayValues.as:14]
          at VO::ArrayValues$cinit()
          at global$init()[C:\FSCalc\Flex\FSCalc\src\VO\ArrayValues.as:3]
          at components::InputForm()[C:\FSCalc\Flex\FSCalc\src\components\InputForm.mxml:168]
          at FSCalc/_FSCalc_InputForm1_i()
          at FSCalc/_FSCalc_Array6_c()
      ...

      What am I doing wrong?

       

      PS, is it normal to take almost 10 minutes to a) go to your discussions, b) read one response to one thread and c) get back to the normal discussion?  That seems excessive to me.

        • 1. Re: static class error
          betheflexcoder Level 2

          If you are trying to achieve singleton,

           

          Your construtor should look like this,

           

          public function ArrayValues(access:SingletonEnforcer)
                  {

           


             if ( access == null )
             {
              throw Error("ArrayValues is called by its instance");
             }

          }

           

          a variable,

           

          static private var _instance  :  ArrayValues;

           

          public static function getInstance():ArrayValues

          {
                if ( _instance == null )
             {
                 _instance = new ArrayValues( new SingletonEnforcer());
             }
             return _instance;

                                  

          }

           

           

           

          ANd another class in the same file,

           

          class SingletonEnforcer
          {
          }

          • 2. Re: static class error
            Jerry62712 Level 1

            Sorry for the late reply.  I've just got back to this project after the usual "an emergency" stuff.

             

            Yes, I'm looking to create a singleton.  I want to pass values around without having to have monster long parameter lists for the various components.

             

            I based the Flex I have on a web page and noted it is similar to the java idea, but not identical based on that page.

             

            I want to make sure I understand the theory behind the code.

             

            public function ArrayValues(access:SingletonEnforcer)
            {
                 if ( access == null )
                 {
                      throw Error("ArrayValues is called by its instance");
                 }
            }
            

             

            This is the constructor for the class which has the same name as the file it is in.  A parameter is passed of the type "SingletonEnforcer" and is then tested to see if it were ever invoked.  If it were, an error is created with a string which will be written to the console.

             

            Next, a variable local to this class is created that will be of the type "ArrayValues" to hold the various arrays.  BTW, either this will have to be expanded or a parallel class created to hold the Spanish values.

             

            static private var _instance  :  ArrayValues;

             

            Next, the actual work horse and true "constructor" is made.  This will either allocate a new instance of the class if it doesn't already exist or instantiate it if it doesn't.  I am confused by the second class, however, when it is created the first time.  This class is included in this file?  Doesn't the file name have to match the class?

            • 3. Re: static class error
              Nathan108

              To reply to your original questions (a and b), the site seems to be very slow at the moment. Login and access to post is slow.

               

              You are getting an error because of the way your Singleton is built. If you look carefully, when your class is initialize, the following line is executed:

              • private static var instance:ArrayValues = new ArrayValues();
              • This will call the constructor: public function ArrayValues() and return the Error.

               

              If you want to create a Singleton pattern you can use the one proposed on the first reply. If you want you can also look at this http://gskinner.com/blog/archives/2006/07/as3_singletons.html.

              • 4. Re: static class error
                kokorito Level 4

                I never open a thread with the left button, use the middle button and open it in a new tab