4 Replies Latest reply on Sep 14, 2009 9:37 AM by JeffryHouser

    Instance of a Class

    tguy895

      How does one call an instance of a class and return that instance inside a function?

        • 1. Re: Instance of a Class
          babo_ya Level 3

          public function GetPerson():Person

          {

               var person:Person = new Person();

           

               return person;

          }

           

          var newPerson = GetPerson();

           

          hope this helps,

          • 2. Re: Instance of a Class
            tguy895 Level 1

            it is a little help. But lets say for example if I have "Class1", how do i call the instance inside lets say, "function1which happends to be inside "Class2"?

            • 3. Re: Instance of a Class
              babo_ya Level 3

              class Class2

              {

                   public function Class2

                   {

               

               

                   }

                   public function function1():Class1

                   {

                        var class1:Class1 = new Class1();

                        return class1;

                   }    

               

               

               

              }

               

              hope this helps,

               

              please mark this post as such.

               

              BaBo,

              1 person found this helpful
              • 4. Re: Instance of a Class
                JeffryHouser Level 4

                I'm not sure what the original poster means by "calling an instance".  But, he may be asking about parameter passing.  In which case something like this may be more appropriate:

                 

                 

                class Class1

                {

                     public function Class1

                     {

                 

                     }

                public function doSomething():void{

                 

                }

                 

                 

                 

                }

                 

                class Class2

                {

                     public function Class2

                     {

                 

                 

                     }

                 

                     public var class1 : Class1;

                 

                     public function function1():void

                     {

                       class1.doSomething();

                 

                     }   

                 

                 

                 

                }

                 

                 

                and a main application like this:

                 

                 

                <mx:Application>

                <mx:Script>

                  public var class1 : Class1 = new Class1();

                </mx:Script>

                <class2 class1 = "{class1}" />

                </mx:Application>

                 

                 

                Then you can access methods and properties on the instance of Class1 in the instance of Class2.