8 Replies Latest reply on Aug 4, 2008 4:48 PM by Gregory Lafrance

    Calling a function from an external AS file

    wkolcz Level 1
      I create a AS file that contains my DAO functions. I imported the class into my application, but what do I have to do to call one of its functions?

      In my ProjectDAO.as I have updateProject(projectID:Number), in my mxml I have a button that calls that function but I get a 'function cannot be found' error.

      What am I missing?
        • 1. Re: Calling a function from an external AS file
          Level 7

          "projectproofing" <webforumsuser@macromedia.com> wrote in message
          news:g777no$odo$1@forums.macromedia.com...
          >I create a AS file that contains my DAO functions. I imported the class
          >into my
          > application, but what do I have to do to call one of its functions?
          >
          > In my ProjectDAO.as I have updateProject(projectID:Number), in my mxml I
          > have
          > a button that calls that function but I get a 'function cannot be found'
          > error.

          If you want to call it like that, you need to include it, not import it.
          Import statements are for classes. If it is a class, then you need to
          either create an instance of the class or make your functions static.

          HTH;

          Amy


          • 4. Re: Calling a function from an external AS file
            wkolcz Level 1
            It is a class so I import it. import edu.umich.ProjectDAO;
            Then I have a variable declaired: public var PDAO:ProjectDAO = new ProjectDAO();
            So then to I have to attached it to the function?

            <mx:Button label="New Project" click="PDAO.addNewProject(event)" />

            • 5. Re: Calling a function from an external AS file
              Level 7

              "projectproofing" <webforumsuser@macromedia.com> wrote in message
              news:g77fm1$414$1@forums.macromedia.com...
              > It is a class so I import it. import edu.umich.ProjectDAO;
              > Then I have a variable declaired: public var PDAO:ProjectDAO = new
              > ProjectDAO();
              > So then to I have to attached it to the function?
              >
              > <mx:Button label="New Project" click="PDAO.addNewProject(event)" />

              Possibly, though just from what it is called it might be a singleton. Care
              to share at least the declaration and constructor? Is there any code
              outside the class or package statements?


              • 6. Re: Calling a function from an external AS file
                Gregory Lafrance Level 6
                In your first post you said you wanted to call updateProject(projectID:Number).

                In your latest post you have this:

                <mx:Button label="New Project" click="PDAO.addNewProject(event)" />

                I think it should be this

                <mx:Button label="New Project" click="addNewProject(event)" />

                Where addNewProject is not in class ProjectDAO, but is the click handler function in the mxml file.

                Then within that addNewProject click handler you call
                PDAO.updateProject(projectID).

                If in fact addNewProject is another method in class ProjectDAO, and is an instance method, then create a click handler like myClickHandler(event) and do this:

                <mx:Button label="New Project" click="myClickHandler(event)" />

                and here is the handler function:

                private function myClickHandler(e:MouseEvent):void{
                PDAO.addNewProject(projectID);
                }

                If instead addNewProject is a class method, call it like this:

                private function myClickHandler(e:MouseEvent):void{
                ProjectDAO.addNewProject(projectID);
                }

                Your actual click handler function may differ, but these are some hints. If none of this makes sense, start by reading FB3 help topic Flex Programming Elements and all its sub-topics, compiling all the sample apps, otherwise you will find it difficult to proceed with Flex.
                • 7. Re: Calling a function from an external AS file
                  wkolcz Level 1
                  Yeah, i screwed the last piece of code up, its not <mx:Button label="New Project" click="PDAO.addNewProject(event)" />, its supposed to read:

                  <mx:Button label="New Project" click="PDAO.createProject(event)" /> which is a function inside my ProjectDAO.as

                  I also changed the import into a <mx:Script source="/edu/umich/ProjectDAO.as">

                  Needless to say nothing has worked yet.
                  • 8. Re: Calling a function from an external AS file
                    Gregory Lafrance Level 6
                    If you use <Script source=... I think you can only import a single source. Your case seems fairly simple. Try to boil this down to a simplified test case and post, should be fairly quick to solve. Post the simplified .as and .mxml.