8 Replies Latest reply on May 19, 2008 8:21 AM by dam85

    Problem about importing class

    dam85
      Hello,
      i have this code.

      <!-- NameProject.mxml -->
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:my="components.*" width="100%" height="100%">

      <mx:Script>
      <![CDATA[
      import lib.User;
      public var u:User = new User();

      trace(u.say("hello world"));
      ]]>
      </mx:Script>
      </mx:Application>


      <!-- /lib/User.as -->
      package lib{
      public class User{
      public function say(s:String):String{
      return s;
      }
      }
      }


      i get this error:

      1120 Access of undefined property

      Why??

      thanks so much
        • 1. Re: Problem about importing class
          peterent Level 2
          You have this statement: trace(u.say("hello world")); but it isn't not inside any function. That may be the problem. I don't know off the top of my head if the error you are getting is a compile-time or runtime error. Which is it?
          • 2. Re: Problem about importing class
            dam85 Level 1
            quote:

            Originally posted by: peterent
            You have this statement: trace(u.say("hello world")); but it isn't not inside any function. That may be the problem. I don't know off the top of my head if the error you are getting is a compile-time or runtime error. Which is it?


            i see this error when i do a 'clear' on my adobe flex builder


            1120 Access of undefined property u.
            • 3. Re: Problem about importing class
              dam85 Level 1
              i wrote the same rows read from adobe flex online manual. :(
              • 4. Re: Problem about importing class
                peterent Level 2
                You have this Script block in your code:
                <mx:Script>
                <![CDATA[
                import lib.User;
                public var u:User = new User();

                trace(u.say("hello world"));
                ]]>
                </mx:Script>

                The trace statement is just "floating" in there - it must be in a function. When do you want this trace executed? For example, if you want to display it when the Application dispatches its creationComplete event, then add a creationComplete event handler to the Application tag and then write the function to handle the event in the Script block:

                <mx:Application ... creationComplete="initApp()">

                <mx:Script>
                <![CDATA[
                import lib.User;
                public var u:User = new User();

                private function initApp() : void {
                trace(u.say("hello world"));
                }
                ]]>
                </mx:Script>

                Now the trace statement appears in a function so you will not get the error.
                • 5. Re: Problem about importing class
                  dam85 Level 1
                  yes, very good! now works!!!

                  thanks so much.....the last question...

                  but if i create an instance of a class in my main.mxml

                  example:
                  public var s:User = new User();

                  i can write a function, like this

                  public function getMyObj():User{
                  return s;
                  }

                  i think this code should works, but how can i call this function (getMyObj()) in other .mxml files?

                  i hate to use the same class instance for every mxml file

                  how could i do?

                  thanks again





                  • 6. Re: Problem about importing class
                    dam85 Level 1
                    ...i have to use.... NOT i "HATE" :)
                    • 7. Re: Problem about importing class
                      peterent Level 2
                      From any class in your application you can do this:

                      import NameProject;

                      var app:NameProject = Application.application as NameProject;
                      var u:User = app.getMyObj();
                      • 8. Re: Problem about importing class
                        dam85 Level 1
                        perfect!!! thanks