5 Replies Latest reply on May 14, 2009 6:28 AM by pyso

    Constructor confusion


      Hi all,

      can anybody explain me how come that you can write this piece of code without any compilator complaint?


      var date:Date = new Date();


      I checked documentation for Date class and I found this:

      Date(yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0, millisecond:Number = 0)

      I made MyDate class with the same constructor as it's written above but when I wrote:
      var myDate:MyDate = new MyDate() I got an error: Incorrect number of arguments. How come?


        • 1. Re: Constructor confusion
          Michael Borbor Level 4

          Probably you forgot to call super on your  custom class constructor.

          • 2. Re: Constructor confusion
            Barna Biro Level 3

            Hi there,


            You are getting that error because you are not passing the needed arguments to it. From that constructor: (yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0, millisecond:Number = 0) you'll need to pass at least 2 parameters for it to work. The parameters from starting from "date" all have a default value. You need to assign a default value to yearOrTimevalue and month too in order to use var myDate:MyDate = new MyDate(); or, you can leave it as it is but you need to pass in the first two parameters.


            With best regards,

            Barna Biro

            Blog: http://blog.wisebisoft.com


            EDIT: one more thing, just because you created a class that requires the same number and type of parameters as an existing class in the framework, doesn't mean that your class will behave the same way. What you don't know is that the constructor of the date class is implemented like this:


            public native function Date(yearOrTimevalue:Object,month:Number,date:Number = 1,hour:Number = 0,minute:Number = 0,second:Number = 0,millisecond:Number = 0);


            You see the keyword "native"? That means that the method is implemented by the Flash Player in native code, something that you can't do in your own classes ( you can't use the "native" keyword in any of the classes you write ). For more information, read up here: http://livedocs.adobe.com/flex/3/langref/statements.html#native

            1 person found this helpful
            • 3. Re: Constructor confusion
              Gregory Lafrance Level 6

              You don't get an error with the Date class because the constructor args have default values, so without args you get the current data, but as Michael points out, if you sub-class this, or probably any class, call the super class constructor first.

              • 4. Re: Constructor confusion
                Barna Biro Level 3

                Not quite correct Greg. The fact that the constructor is "native" is the reason why you are not getting any errors ( meaning that it's implemented in native code somewhere else too where everything that it needs to know is either defined or passed ). If you take a closer look then you'll see that the first 2 parameters DO NOT have a default value. In a normal situation, you'd be forced to pass those values in order to instantiate a class ( just a write a fake class, see that you won't be able to instantiate it without passing values to the parameters that do not provide a default value ). The Date class is not "an ordinary class", that's why it can be instantiated without the need of passing 2 parameters that under normal conditioins MUST be passed.


                In case he did use inheritance to create the new class, then yeah, calling super should work but that's not what he said ( at least, that's not what I understand by "I made MyDate class with the same constructor as it's written above" ).

                • 5. Re: Constructor confusion
                  pyso Level 1

                  yes, you understood my sentence correctly :-)

                  Now it's a little bit clearer for me.