6 Replies Latest reply on Jun 20, 2011 2:36 AM by EvyatarBH

    Typecasting problem from int to CustomObject Class

    BhaskerChari Level 4

      Hi Guys,


      I have run into one strange requirement where I want to convert my int values to my CustomObject class which doesn't work and throws a compiler error "1067: Implicit coercion of a value of type int to an unrelated type GenericObject" where GenericObject is my cutsom object class.


      Let me explain in detail...


      Say I have created my custom class GenericObject which is also a dynamic class extending the Object class ... and I am doing nothing in that class just but extending the Object class which is as shown below..


      public dynamic class GenericObject extends Object {
              public function GenericObject() {


      Now I'll tell you what is the actual problem...


      public function myFunction(obj:Object):void{

           //My logic here



      myFunction(121); //Function call


      Now I am calling the above function by passing an integer as a parameter which has no problem..and it is typecasted implicitly without any compilation errors...and value 121 is stored in obj...


      But if I use my custom class which is already extending Object class it starts hitting me with an error...Is there any way to eliminate/avoid that error or is there a way to override typecasting before it throws any error...however I needed to use my CustomClass GenericObject" instead of normal Object datattype in the function.


      public function myFunction(obj:GenericObject):void{

           //My logic here



      myFunction(121); //Function call....throws a compilation error as explained above


      Thanks for your time and patience.


      Any help or insight into this problem will be greatly appreciated.

        • 1. Re: Typecasting problem from int to CustomObject Class

          You should review the basic of OOP principle of inheritance.

          For example I have 3 classes A, B and C; B extends A, C extends A. In this situation i can cast an instance of B to type A, cast an instance of C to type A, but I can't cast an instance of B to type C, vice versa.

          • 2. Re: Typecasting problem from int to CustomObject Class
            BhaskerChari Level 4

            Hi harihisu,


            First of all thanx for your reply...However in the example which you gave B and C are no where related but they are individually inheriting from class A...and which is the not the case which I have explained if you read it properly.


            What I wanted to have is when I can typecast an int to Object class why I cannot have a type conversion from int to my custom class GenericObject which is also extending an Object class(It is just extending the class and doing nothing more than that)



            • 3. Re: Typecasting problem from int to CustomObject Class
              harihisu Level 2

              No matter if you override some functions inside GenericObject or not, whenever you extends the Object class, the new class will be considered as successor of Object, not the same (even they do defenitely the same thing). You can try my example with 3 classes A,B,C inherits each other (leave B blank like your GenericObject) and do the typecasting (with A means Object, B means GenericObject and C means int).


              var b:A = new B();

              var c:A = b as C; <== Same Error right?

              • 4. Re: Typecasting problem from int to CustomObject Class
                UbuntuPenguin Level 4

                You can't do that, you can only pass objects that have GenericObject in their heirarchy.  Both int and GenericObject extend Object ( I don't think you need the "extends Object" in the declaration) so any function that has ":Object" or ":*" in its paramter list will be able to take both.  However, when you specifiy GenericObject only classes that have GenericObject ( Foo extends GenericObject ) in their heirarchy will be accepted without throwing an error.  The int object DOES NOT extend GenericObject or extend any class that does, so it is impossible for it to be accepted as a valid parameter in any function that requires GenericObject


                Consider the following function.

                public function feed( value:Animal ):*

                This function  can accept a parameter that is a duck or a human baby.  Why, because ducks and humans both have the Animal class in their heirarchy.

                Now consider this function



                public function putInOvenAt300FWithSeasonSalt( value:EdibleMammal ):


                This function will still work for the duck ( even though ducks aren't mammals but whatever, they are for this example ). Now if you try to pass a human baby to this function you will have some problems, well why ?  Because even though ducks and human babies both have the Animal class in their heriarchy, they don't both extend the EdibleMammal class. Flex responds by throwing up nasty error messages because you tried to cook a baby.

                • 5. Re: Typecasting problem from int to CustomObject Class
                  BhaskerChari Level 4

                  Hi Ubuntu,


                  Thanks for the thorough response...However I do understand the Class Inheritance hierarchy...But I just want to do something how int is getting converting to implicit Object like the same way is there any possibility to do that.


                  Actually let me explain what am I doing in background what made me to post this..


                  We are using Flex PMD tool in order to make sure we are writing an efficient and quality code...Hope you are aware of Flex PMD. So when I try to run my application with this tool it shows some errors related to using the dynamic Object class...as shown below:


                  Do not use Object class. It is a bad practice to use the dynamic class Object. Prefer using strongly typed object, or marker interface in order to avoid silent compilation errors while refactoring



                  So in order to avoid this error I have created a new GenericObject class which extends the Object class as posted earliar..however it does removes the Flex PMD errors when I try to replace the usage of builtin Object class with my GenericObject class but it is only applicable for all the cases when the receiveing type is the Object indeed but not int or string to the function whose datatype is GenericObject...however it works fine if it is normal object instead of my GenericObject....As explained with an example in my previous post.


                  How can I avoid such kind of a Flex PMD error is there any alternative approach to do that...DO you have any ideas..??



                  • 6. Re: Typecasting problem from int to CustomObject Class
                    EvyatarBH Level 3

                    If you don't add anything meaningful in your GenericObject class and the only purpose of it is to eliminate code-inspection errors of Flex PMD, then you don't need this class and creating redundant classes sounds to me like a bad practice as well.


                    Check the possibility of changing the Flex PMD rules.

                    That way you should be able to make that error a warning or just remove it.


                    Anyway, you can't change the class heirarchy of core types and it sounds a bit like you don't know which variables are being passed around (if a method needs an int variable, provide an int variable, why passing an Object in the first place?).