Using Getters and Setters on Dates in ActionScript 3

Okay, I ran into this issue and I’m hoping someone smarter than me (that’s a lot of you) has either run into this before or knows what the deal is. I think it’s probably something simple but in Googling it I turned up no results so I wanted to post here in case anyone else has issues in the future. Here’s the deal.

I’m using the sweet, out of the box, getters and setters and I’ve got one for date/time:

[as]public function set time (value:Date) : void

{

this._time = value;

}

public function get time () : Date

{

return this._time;

}

[/as]

With strings or any other variable type, that works fine. But when I try to create a date my time variable is always null. I’ve done it a couple of different ways to no avail. Try 1 (time is a property of the Waypoint class:

[as]

waypoint.time = new Date();
waypoint.time.setUTCFullYear(year, month, day);
waypoint.time.setUTCHours(hours, minutes, seconds);

[/as]

Try 2:

[as]

var tempDate : Date = new Date();
tempDate.setUTCFullYear(year, month, day);
tempDate.setUTCHours(hours, minutes, seconds);
waypoint.time = tempDate;

[/as]
I thought that might fire the setter correctly but time is still null after going through the function. Finally I had to set time to a public property and I could use method 1 from above just fine.

Anyone know what I’m doing wrong and how to fix it?

[tags]ActionScript3, getters and setters[/tags]

Related posts:

  1. ActionScript Problem
  2. Multi-touch Flash (Think Microsoft Surface with Actionscript 3)
  3. Peter Fisk’ C# and ActionScript Performance Test
  4. Essential ActionScript 3 is the Bestselling O’Reilly Book Ever
  • http://www.munkiihouse.com Tony Fendall

    Sounds more like a bug, than you actually doing anything wrong :)

    I don’t know what the problem is exactly, but the easy work around is to put all your values into the date constructor instead. Eg:
    new Date( year, month, day, hour, minute, second, millis);

    After that, the time property is set correctly.

  • http://www.munkiihouse.com Tony Fendall

    I think I completly miss understood your question. If I read you correctly the second time around, then there is something really weird going on.

    I can’t recreate the bug at all, but maybe the compiler is doing a ‘call by value’ to your function, rather than a ‘call by reference’?

  • http://www.capitalhcoder.com Shaun Halberstadt

    I’ve had some weirdness similar to this happen before where a function would get called, but it would not get stepped into (could be happening here?). I tried changing things many different ways, until I decided I knew my code was right, so I did a clean project and cleared out the browser, and everything started working again…

    I’m not sure why, but you might try cleaning your project and clearing your browser cache and see if it fixes things for you.

  • Dave G

    Does the Waypoint class have a property named “value”? If so, that might be hiding the argument named “value”.

  • http://www.bobjim.com Ryan Campbell

    I tried you code, definitely worked for me. One thing to note is if there is a problem calling the setter function (which there shouldn’t be in your case) no error is thrown.

    Try adding a break point in the setter function and step through the execution 1 by 1 to see if there are any errors.

  • http://blog.digitalbackcountry.com Ryan Stewart

    Hmm, thanks guys. Are you trying the second or the first try? I still can’t set time to anything (it always ends up being null) but maybe it’s something else in my code if it’s working for you guys.

  • http://www.yourminis.com Jeremy

    Hey Ryan, i tried using the first try (using the setter with a new date) and it works fine.

    btw, you can call the getters and setters “accessors”.

  • http://neatfilm.com George


    waypoint.time = new Date();
    waypoint.time.setUTCFullYear(year, month, day);
    waypoint.time.setUTCHours(hours, minutes, seconds);

    Should be:

    var time:Date = new Date();
    time.setUTCFullYear(year, month, day);
    time.setUTCHours(hours, minutes, seconds);
    waypoint.time = time;

  • http://neatfilm.com George

    Sorry, wrong code myself. :)

    I tried again. Your code should no anything wrong.

  • http://www.johncblandii.com John C. Bland II

    Ryan, I’ve tested and it works fine. Here’s my class:

    package
    {
    public class Waypoint
    {
    private var _time:Date;
    public function get time():Date{ return _time; }
    public function set time(value:Date):void{ _time = value; }
    }
    }

    Here’s my tester:

  • http://www.johncblandii.com John C. Bland II

    Ok, my tester file code didn’t show up (didn’t think it would). Here’s the function out of my mxml file.

    private function init():void{
    var waypoint:Waypoint = new Waypoint();
    waypoint.time = new Date();
    blah.text = waypoint.time.toDateString();
    }

  • http://blog.digitalbackcountry.com Ryan Stewart

    Sweet, you guys rock, found the issue. I’m an idiot and had a typo in my accessors.

    I need a vacation :)

  • Dave G

    What was the typo? I don’t see it.