4 Replies Latest reply on Apr 22, 2010 2:09 PM by RandomReado

    Calling methods from custom class?

    RandomReado Level 1

      Here's what I have:

       

      src\MainApp.mxml
      src\components\engine\Posts.as
      

       

      In Posts.as there's a public function "addPost()".

       

      In the MainApp.mxml I've specified:

       

      import components.engine.Posts;

       

      However when I try to use "addPost()" as a click handler for a button, Flex says:

       

      "1180: Call to a possibly undefined method addPost"

       

      Can anyone tell me why? Thanks in advance.

        • 1. Re: Calling methods from custom class?
          levancho Level 3

          you need either instance of Posts or make the function addPost as static

          both way you have to qualify method with intsance name or clas name

           

          var myinstance:Posts = new Posts();

          myinstance.addPost()

           

          or if its static

           

          Posts.addPosts();
          1 person found this helpful
          • 2. Re: Calling methods from custom class?
            RandomReado Level 1

            I see what you mean. I just changed addPost() to:

             

            public static function addPost(id:Number):void
            {
            }
            

             

            Then in the click handler:

             

            Posts.addPost(1)

             

            This seemed to work. However inside the addPost method is a call to the HTTPService, and I have a result and fault handler, both are pointing to private methods inside the Posts.as file. When I applied the static to the addPost() method, Flex said it could no longer see the handler methods, both of which were private. When I made them static the error went away.

             

            Do you know why this is?

            • 3. Re: Calling methods from custom class?
              levancho Level 3

              In that case you need instance of posts not statix function

               

              -- Sent from my Palm Pre

              On Apr 22, 2010 4:20 PM, RandomReado <forums@adobe.com> wrote:

               

              I see what you mean. I just changed addPost() to:

               

               

               

              public static function addPost(id:Number):void

               

              {

               

              }

               

               

               

               

               

              Then in the click handler:

               

               

               

              Posts.addPost(1)

               

               

               

              This seemed to work. However inside the addPost method is a call to the HTTPService, and I have a result and fault handler, both are pointing to private methods insode the Posts.as file. When I applied the static to the addPost() method, Flex said it could no longer see the handler methods, both of which were private. When I made them static the error went away.

               

               

               

              Do you know why this is?

              • 4. Re: Calling methods from custom class?
                RandomReado Level 1

                Ah Ok. Thanks.