8 Replies Latest reply on Jun 19, 2009 2:55 PM by JennHysuick

    ActionScript question...

    JennHysuick Level 1

      I have 3 classes in my project, a user, a specialUser (which extends user), and adminUser (which extends specialUser).

       

      When a person logs into the project, currently I'm creating one of three objects, a user, a specialUser or an adminUser, depending on which they log in as.  What I want is to have 1 public variable for theUser, but I don't know how to type it when I create it (and I don't think I can create public variables in a function).  Having one user object to deal with throughout the code would be better than having to do a series of if statments each time I need to use it (if user then... else if specialUser then...else if adminUser then...).

       

      Is there any way I can do this?  I'd like to think there is some simple way to code this, and perhaps I just haven't had enough coffee yet to figure it out.

       

      Thanks!

       

      Jenn

        • 1. Re: ActionScript question...
          _Natasha_ Level 4

          Hi Jenn,

          I think flash.utils.getDefinitionByName is what you need.

          • 2. Re: ActionScript question...
            BennyBeta Level 4

            Here's an example:
                       
                        var clazz:Class = getDefinitionByName( className ) as Class;
                       
                        if( clazz )
                        {
                            var object:Object = new clazz();
                            return object;
                        }
                        else
                        {
                            throw new Error( className+" is not a valid Class." );
                        }

            • 3. Re: ActionScript question...
              injpix Level 3

              Using the reflection method getDefinitionByName in the flash.utils package, will return an Object type which you will then need to create a Class instance that will cast it.  Using getQualifiedClassName will return a String type of the name of the class.  And getQualifiedSuperclassName will return a String type of the name of the base class.

               

              However, you can also use a public static const that indicates the class name and will get "pushed" to the super class, so that all subclasses and the base will be known.  Below is a snippet of ChatViewEvent, a subclass from Event typeNow having the type assigned to the super class, I will know what type any subclasses are by accessing the that property: myChatViewInstance.type //returns "chatViewEvent"


                  public class ChatViewEvent extends Event
                  {
                      public static const TYPE:String = "chatViewEvent";


                      public function ChatViewEvent(  bubbles:Boolean = false,
                                                                     cancelable:Boolean = false)
                      {
                          super(
              TYPE, bubbles, cancelable);
                      }


                       //...more code

               

               

              Does anyone know if reflections are expensive?

              • 4. Re: ActionScript question...
                GordonSmith Level 4

                You say that SpecialUser extends User and AdminUser extends SpecialUser. (BTW, the convention is to capitalize class names and if you don't your posts will be confusing.) Therefore a SpecialUser or an Admin User is just a specialized kind of User, so you can just declare

                 

                public var user:User;

                 

                and later set

                 

                user = new User();

                or

                user = new SpecialUser();

                or

                user = new AdminUser();

                 

                In other words, a variable typed as some class C can always refer to an object which is actually an instance of a subclass of C.

                 

                Gordon Smith

                Adobe Flex SDK Team

                • 5. Re: ActionScript question...
                  JennHysuick Level 1

                  Thank you Gordon!

                   

                  I've been on holidays since the end of May, but saw your response and tried it while I was gone (yeah - I work on my "vacation").  Worked like a charm!

                  • 6. Re: ActionScript question...
                    JennHysuick Level 1

                    Gordon,

                     

                    I'm not entirely sure what has happened with my code.  I have finally picked up where I left off, but now the suggestion you made is not working for me.  In my main.mxml, I have declared

                     

                    public var theUser:UserClass;

                     

                    my three class files are:

                     

                    1.

                    package user

                    {

                          //variables in here

                         public class UserClass

                           {

                              //variables in here

                            }

                    }

                     

                    2.

                    package user

                    {

                         public class SpecialUserClass extends UserClass

                         {

                             //variables here

                         }

                    }

                     

                    3.

                    package user

                    {

                         public class AdminUserClass extends SpecialUserClass

                         {

                              //variables

                              public function popAdminCourses():void

                              {}

                         }

                    }

                     

                    And then in my code, I have:

                         theUser = new AdminUserClass();

                         theUser.popAdminCourses();

                     

                    and I get the error "Call to a possibly undefined method popAdminCourses through a reference with static type user:UserClass".

                     

                    Am I missing something, did I delete something?  I'm at a loss right now.

                     

                    Thanks!

                     

                    Jenn

                    • 7. Re: ActionScript question...
                      GordonSmith Level 4

                      The compiler doesn't understand that at runtime you are putting an instance of AdminUserClass into a variable of type UserClass. It's worried that since at compile time you declared 'theUser' to be of type UserClass, it might at runtime be ANY kind of user -- an instance of UserClass, or an instance of SpecialUserClass, or an instance of AdminUserClass. And it knows that two of these don't have a popAdminCourses() method. So it doesn't want to let you call it because if theUser was the wrong kind of object and you call a method that doesn't exist on it, the Flash Player will throw a runtime exception.

                       

                      To tell it to treat theUser as an AdminUserClass and let you call the popAdminCourses() method, you use a "cast":

                       

                      theUser = new AdminUserClass();

                      AdminUserClass(theUser).popAdminCourses();

                       

                      If you're in some part of the code where you can't be sure what kind of user theUser holds, then you need to write code like

                       

                      if (theUser is AdminUserClass)

                          AdminUserClass(theUser).popAdminCourses();

                       

                      because if you try to cast theUser to the AdminUserClass type and it isn't one, you'll also get a runtime exception.

                       

                      BTW, most people don't put "Class" at the end of their classnames. I't would be normal to just call them User, SpecialUser, and AdminUser. For example, in the Flex framework we don't have ButtonClass, we just have Button.

                       

                      Gordon Smith

                      Adobe Flex SDK Team

                      1 person found this helpful
                      • 8. Re: ActionScript question...
                        JennHysuick Level 1

                        Thank you so much for your quick response.  I was hoping to try to avoid testing if the theUser was Admin, Special or User, but I guess there is no getting around it!

                         

                        Made the changes you mentioned, and once again I am error free!

                         

                        Jenn