6 Replies Latest reply on Nov 3, 2008 3:53 PM by Newsgroup_User

    Class Inheritance/Casting Question

    ericbelair Level 1
      I have two classes that extend a base class:

      public class MyBase {}

      public class MyFirstChild extends MyBase {}

      public class MySecondChild extends MyBase {}

      In one of my components, I am casting both child classes as the base class, but at one point, I need to know which instance is being referenced (i.e. a filter function for a Collection).

      Right now, I am handling it like this:

      var myBase:MyBase; // used to store a selected item in a Tree

      private function filterFunction(item:Object):Boolean
      {
      if (myBase is myFirstChild)
      return (item is myFirstChild);
      else if (myBase is mySecondChild)
      return (item is mySecondChild);

      return true;
      }

      This is a simplified version of my code, but, I feel like there should be a better/simpler way to do this - something more dynamic, and not tied to Class names (for instance, what if I add another Class that extends MyBase?).

      Any suggestions?

      Thank you.
        • 1. Re: Class Inheritance/Casting Question
          dmindler Level 1

          Advanced ActionScript 3 with Design Patterns

          Basically, define in MyBase a method called filterFunction() that throws an Error

          Then, in each sub-class, define a filterFunction() as an override with class specific implementation.

          • 2. Re: Class Inheritance/Casting Question
            Level 7

            "ericbelair" <webforumsuser@macromedia.com> wrote in message
            news:genlpu$69q$1@forums.macromedia.com...
            >I have two classes that extend a base class:
            >
            > public class MyBase {}
            >
            > public class MyFirstChild extends MyBase {}
            >
            > public class MySecondChild extends MyBase {}
            >
            > In one of my components, I am casting both child classes as the base
            > class,
            > but at one point, I need to know which instance is being referenced (i.e.
            > a
            > filter function for a Collection).
            >
            > Right now, I am handling it like this:
            >
            > var myBase:MyBase; // used to store a selected item in a Tree
            >
            > private function filterFunction(item:Object):Boolean
            > {
            > if (myBase is myFirstChild)
            > return (item is myFirstChild);
            > else if (myBase is mySecondChild)
            > return (item is mySecondChild);
            >
            > return true;
            > }

            This is going to return true no matter WHAT you do ;-), so no need to check.

            Could you be a bit more clear on exactly what you're trying to accomplish?
            I think dmindler is on to something, that you need to have a common method
            or property that works specifically for that class, but since you're already
            _in_ the filterFunction, I'm doubting it needs to be a filterFunction.

            It's possible that using an Interface would be prefereable to the "abstract
            class" he/she advocates, but without knowing more specifics it's hard to
            say.


            • 3. Re: Class Inheritance/Casting Question
              ericbelair Level 1
              Ok. Let's say I have a Tree with an ArrayCollection dataProvider that has folders and nodes. The "folders" are instances of the Custom Class "Folder", and the nodes are instances of the Custom Class "Node". Both Classes extends the Custom Class "TreeItem". The "Folder" class has a "children" property that can contain both folders and nodes.

              Now, at several points, I need to know if a specified item in the Tree (perhaps while looping through the children of a Folder instance) is of the same Class as the selectedItem, which can be either a Folder or Node. I am basically doing the following:

              public function isItemTheSameType(item:Object):Boolean
              {
              if (tree.selectedItem is Folder && item is Folder)
              return true;
              if (tree.selectedItem is Node && item is Node)
              return true;

              return false;
              }

              But, this is not dynamic. I'd like to find some way to say "if this item is the same class as this other item, do this", that way, it's something that I can reuse, and not have to be typed to the classes i specify.
              • 4. Re: Class Inheritance/Casting Question
                Level 7

                "ericbelair" <webforumsuser@macromedia.com> wrote in message
                news:genpj5$b8j$1@forums.macromedia.com...
                > Ok. Let's say I have a Tree with an ArrayCollection dataProvider that has
                > folders and nodes. The "folders" are instances of the Custom Class
                > "Folder",
                > and the nodes are instances of the Custom Class "Node". Both Classes
                > extends
                > the Custom Class "TreeItem". The "Folder" class has a "children" property
                > that
                > can contain both folders and nodes.
                >
                > Now, at several points, I need to know if a specified item in the Tree
                > (perhaps while looping through the children of a Folder instance) is of
                > the
                > same Class as the selectedItem, which can be either a Folder or Node. I am
                > basically doing the following:
                >
                > public function isItemTheSameType(item:Object):Boolean
                > {
                > if (tree.selectedItem is Folder && item is Folder)
                > return true;
                > if (tree.selectedItem is Node && item is Node)
                > return true;
                >
                > return false;
                > }
                >
                > But, this is not dynamic. I'd like to find some way to say "if this item
                > is
                > the same class as this other item, do this", that way, it's something that
                > I
                > can reuse, and not have to be typed to the classes i specify.

                How bout

                if (tree.selectedItem.className==item.className) return true;

                HTH;

                Amy


                • 5. Re: Class Inheritance/Casting Question
                  ericbelair Level 1
                  That doesn't work, but you gave me the idea to try this, which does work:

                  return (getQualifiedClassName(reportHierarchyItem) == getQualifiedClassName(item));

                  Thanks.
                  • 6. Re: Class Inheritance/Casting Question
                    Level 7
                    As long as you found the solution ;-)

                    "ericbelair" <webforumsuser@macromedia.com> wrote in message
                    news:gentkn$gth$1@forums.macromedia.com...
                    > That doesn't work, but you gave me the idea to try this, which does work:
                    >
                    > return (getQualifiedClassName(reportHierarchyItem) ==
                    > getQualifiedClassName(item));
                    >
                    > Thanks.