1 Reply Latest reply on Sep 30, 2010 12:29 AM by Ratsnackbar

    create a class

    jagguy999 Level 1

      I have been having problems with creating a class with actionscript project in flashbuilder.

      Here is a simple class but how do I use this with the skeleton code here?
      How do I arrange these files to make this work?

      Here I instatiate the class
      var p1 : Person2 = new Person( 1, "Xavi", "Beumala", "address1");

      Here is the Skeleton code
      package
      {
            import flash.display.Sprite;

            public class atest2 extends Sprite
            {
                 
                 
                  public function atest2()
                  {
                       
                  }
            }
      }

       

      public class Person2 {
                
                private var _id : int;
                
                public var name : String;
                public var surname : String
                public var street : String;
                
                public function Person2( id : int, name : String = "", surname:String = "", streetString="") {
                     this._id = id;
                     this.name = name;
                     this.surname = surname;
                     this.street = street;
                }
                
                public function get id() : int {
                     return _id;
                }
           }
      
        • 1. Re: create a class
          Ratsnackbar Level 2

          Perhaps I am a bit confused because of the way you display your code here.  Are you creating the second class within the same file as the first?  If so that second class will only be accessable from within the first one and not to any code outside the primary class.  I.E. each class needs to be in their own seperate files.  If the Person class is in a different package you will also need to import it into the calling class.  (I assume you already know this but am just making sure.)

           

          Here is your person class slightly modified so that you can retrieve each vars data using a function and keep them private.  I would also suggest setting the data on each field using a setter function so that you can scrub the incomming data but that's up to you.

           

          package {

          public class Person{
           
            private var _ID:int;
            private var _name:String = "";
            private var _surname:String = "";
            private var _street:String = "";

           

            public function Person(id:int, name:String, surname:String, street:String){
                 this._ID = id;
                 this._name = name;
                 this._surname = surname;
                 this._street = street;
            }
           
            public function get id():int{
             return this._ID;
            }
           
            public function get name():String{
             return this._name;
            }
           
            public function get surname():String{
             return this._surname;
            }
           
            public function get street():String{
             return this._street;
            }
          }
          }

           

          And to test it you can trace out the function call or (my preference) import Alert and ObjectUtil to pop up the object data in an alert box.  These are part of the Flex MX libraries though so you may not want to use them if your writing pure actionscript code.  Of course you would want to run this code out of another class.

           

          // import the libraries needed to use Alert boxes and convert the object to a string.  If your Person class was in another package you would want

          // to remember to import it also.

           

          import mx.controls.Alert;
          import mx.utils.ObjectUtil;

          //Create your Person variable to use locally.

          private var myPerson:Person;

          //A function to build a person and test what is returned by showing the details in an alert box.

            

          public function testPerson():void{
              this.myPerson = new Person(1,"John", "Doe", "1234 Anywhere Street");
              Alert.show(this.myPerson.id + " " + this.myPerson.name  + " " + this.myPerson.surname + " " + this.myPerson.street);
          }

           

           

          Hope that helps.  Let me know if that does not make sense or if perhaps I missunderstood your question.

           

          -Joe