18 Replies Latest reply on Dec 4, 2009 12:58 AM by paul.williams

    Error with new Package

    joob23

      I am trying to retrieve a variable from an .as file from one mxml file, while setting it from another.  I thought I had everything set up, but I am retrieving an error.  1119: Access of possibly undefined property loc through a reference with static type Class. I'm assuming I am missing something, but can't figure out what it is.

       

      Any help is greatly appreciated.

       

       

      This is how the value is set within the mxml

      PickLocation.loc = cboPreviousLocations.value.toString();

       

      I have imported the package.

       

       

       

       

      import

       

       

      widgets.PickLocation;

       

      The .as file

       

       

       

       

       

       

       

       

       

      package

       

       

      [

      Bindable]

       

       

      public class PickLocation{

       

       

       

      public var _loc:String;

       

       

      public function get loc():String {

       

       

      return _loc;

      }

       

       

      public function set loc(value:String):void {

      widgets{

      _loc = value;

      }

       

      }

      }

        • 1. Re: Error with new Package
          satyajitbehera

          You can't set the "loc" variable like that.

           

          Just create an instance of PickLocationa and then set it, like -

          var pl:PickLocation = new PickLocation();

          pl.loc = cboPreviousLocations.value.toString();

           


          Alternatively if you want to access the property as a Class variable you need to declare that static in the class file PickLocation. Do it this way -

           

          public static var _loc:String;

           

          public static function get loc():String { 

               return _loc;

          }

           

           

          public static function set loc(value:String):void {

          _loc = value;

          }

           

          and now you can access the loc property as you were trying to, i.e. -

          PickLocation.loc = cboPreviousLocations.value.toString();

           

          I hope this helps.

          • 2. Re: Error with new Package
            c3eb4

            The reason you can't set it that way (as the previous poster noted) is because the property is not static.  The way you are trying to set the .loc property requires the .loc property to be static.

            • 3. Re: Error with new Package
              joob23 Level 1

              public static function set loc(value:String):void {

               

               

               

               

              _loc = value;

              }

               

              On the above code, I am now receiving the following error:

               

              1042: The this keyword can not be used in static methods. It can only be used in instance methods, function closures, and global code.

              • 4. Re: Error with new Package
                c3eb4 Level 1

                Your _loc will also need to be static.  Also make sure that a static property is what you intend on achieving.  Let me know if you want more information on the repercussions of static members.

                • 5. Re: Error with new Package
                  satyajitbehera Level 1

                  just use the code I have posted earlier. You need to define static in 3cases

                  - where you declare the variable

                  - in the getter of the variable

                  - in the setter of the variable

                  • 6. Re: Error with new Package
                    joob23 Level 1

                    I put it in exactly as you have it.  Now I get the new error as stated.

                    • 7. Re: Error with new Package
                      joob23 Level 1

                      Here is what I have and the error I am receiving.

                       

                      Error 1042: The this keyword can not be used in static methods.  It can only be used in instance methods, function closures, and global code.

                       

                       

                      package

                       

                       

                       

                       

                       

                      {

                      [

                      Bindable]

                       

                      public class PickLocation

                      {

                       

                      static public var _loc:String;

                       

                      static public function get loc():String {

                       

                      return _loc;

                      }

                       

                       

                      static public function set loc(value:String):void {   *****This is where the error is occurring.

                      _loc = value;    }

                       

                      }

                      }

                       

                      I'm not following why I'm receiving the error considering I'm not using the this keyword anywhere.

                      • 8. Re: Error with new Package
                        c3eb4 Level 1

                        Try

                         

                        PickLocation._loc = value;

                         

                        in your setter function.  When you reference a variable without explicitly supplying scope, the 'this' keyword is assumed on the variable.  That's why the error seems a little strange, but in fact it does make sense.  Since _loc is static, prepending it with it's class name should resolve your issue.

                         

                        Note, you probably need to do the same thing in your getter function (return PickLocation._loc).

                        • 9. Re: Error with new Package
                          joob23 Level 1

                          Tried but that did not resolve the issue.

                          • 10. Re: Error with new Package
                            Subeesh Arakkan Level 4

                            Hi,

                             

                            The class need to be instantiated to use the this keyword, so it is not available in static methods.

                             

                            Check this modification

                             

                            package
                            {
                                [Bindable]
                                public class PickLocation
                                {
                                    private var _loc:String;

                             

                                    public function get loc():String
                                    {
                                        return _loc;
                                    }

                             

                                    public function set loc(value:String):void
                                    {
                                        _loc = value;
                                    }
                                }
                            }

                             

                            To use the class,

                            var pickLocation:PickLocation = new PickLocation();
                            pickLocation.loc = "Test";
                            

                             

                            Message was edited by: Subeesh

                            • 11. Re: Error with new Package
                              paul.williams Level 4

                              You'll have to remove the class-level [Bindable] metadata it you want to have a static getter / setter in your class.

                              • 12. Re: Error with new Package
                                joob23 Level 1

                                The set seems to work as I can retrieve the value while I am in the component.  However, when I am calling the value from a different component after the value has been set, I receive the default value.  Any ideas?

                                • 13. Re: Error with new Package
                                  c3eb4 Level 1

                                  I strongly recommend you read about static members:

                                  http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/statements.html#st atic

                                  You should be able to figure out the correct way to approach your problem after understanding what static really means.  It's hard to even tell if you want this class to be static or not, so giving you an appropriate solution is like throwing darts blindfolded.

                                   

                                  *edit: specifically, "Specifies that a variable, constant, or method belongs to the class, rather than to instances of the class."

                                  • 14. Re: Error with new Package
                                    joob23 Level 1

                                    Sorry for all the troubles with this.  I'm a noob when it comes to Actionscript/Flex as if you couldn't tell already.

                                    After looking through your link and other such pages, I would like it to be static as I will be calling the variable from many different components and it appears that would be the best way so as to not create instances every time I need the variable.  Which still leaves me wondering why my set isn't holding when the variable, get, and set are all static.

                                    • 15. Re: Error with new Package
                                      joob23 Level 1

                                      I think it may be an instantiation issue.  In debugging, the set value does not show the get function is called.

                                      • 16. Re: Error with new Package
                                        paul.williams Level 4

                                        If you can post a simple example that demonstrates your problem then we may be able to provide further assistance.

                                        • 17. Re: Error with new Package
                                          joob23 Level 1

                                          Alright hopefully I'm able to explain this completely.  First, I'm using Flex to create ESRI ArcGIS Server web application.  They have a prebuilt viewer that you can modify for your application.  The main application contains a container that consists of multiple components.  Within one of these components contains a widget manager.  This manager retrieves a list of widgets from a config.xml.  The widgets are created when selected out of a menu.  However, I'm going to have one component that is preloaded.  This component with have a combobox that will have a value selected.  I want this value to be passed into my .as file to be stored and to be retrieved when the next widget is opened.  Does this help?  Hopefully it does if not I'll try to explain better.

                                          • 18. Re: Error with new Package
                                            paul.williams Level 4

                                            Sorry I meant some code that demonstrates the issue you were describing.