A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Casting to Date doesn't work ?!

  1. #1
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560

    Casting to Date doesn't work ?!

    Hm, i am trying to cast a variable of the type Object to the type Date

    PHP Code:
    var cookie SharedObject.getLocal("50FirstDates");
    cookie.data.myDate Object(new Date(1969,11,30));
    var 
    d:Object cookie.data.myDate;
    if (
    instanceof Date) {
        
    trace(Array(d));// works
        
    trace(String(d));// works
        
    trace(Number(d));// works
        
    trace(Date(d));// doesn't work

    I got my variable "d" here, which is actually an instance of Date
    and can't use it as a Date, what am i missing?

    Any ideas for a workaround?

    [edit]
    In line 2 i need the date stored as an Object,
    as i use a custom cookie handler class...
    Last edited by McUsher; 11-26-2005 at 08:56 AM.
    My letters on the F1 key have faded, how are yours today?

  2. #2
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    Ok i got a workaround
    i put another getter: cookie.getAsDate(name:String):Date
    in my CookieHandler class..


    But still i'd like to know how i can cast an Object which is an instance of Date
    to a Date..
    My letters on the F1 key have faded, how are yours today?

  3. #3
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Why are you casting it to an Object first? Why ot just use:
    code:

    cookie.data.myDate =new Date(1969,11,30);
    var date = cookie.data.myDate;



    K.

  4. #4
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    I know that..
    The thing is, i have a CookieHandler class, which stores everything as an Object.

    It must be possible to cast an Object of the instance Date to a Date.. just .. how?
    My letters on the F1 key have faded, how are yours today?

  5. #5
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    The thing is, in Flash everything is an Object...Object is the base datatype from which every other type is subclassed...so if your CookieHandler class is expecting an Object, you can pass it a Date object without it complaining...

    K.

  6. #6
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    I know that, the point is:
    Quote Originally Posted by FlashHelp
    ActionScript 2.0 lets you cast one data type to another. Casting an object to a different type means you convert the value that the object or variable holds to a different type.

    The results of a type cast vary depending on the data types involved. To cast an object to a different type, you wrap the object name in parentheses (()) and precede it with the name of the new type. For example, the following code takes a Boolean value and casts it to an integer.

    var myBoolean:Boolean = true;
    var myNumber:Number = Number(myBoolean);
    But today i found another passage about casting:
    The syntax for casting is type(item), where you want the compiler to behave as if the data type of the item is type. Casting is essentially a function call, [...]

    Ok.. i bet they are talking about a constructor (function) call.
    And as the constructor of the object Date, doesn't allow you passing a
    Date in, you can't cast it with Date(instanceOfADate).

    But, strange enough even calling the cast correctly:
    var d:Object = cookie.data.myDate;
    var date:Date = Date(d.getTime());

    Only:
    var date:Date = new Date(d.getTime());
    will work..

    I am not sure, if the Date Object is working as inteded.
    (But maybe i got something wrong.. will ask my Java Geeks later)
    Last edited by McUsher; 11-30-2005 at 07:01 AM.
    My letters on the F1 key have faded, how are yours today?

  7. #7
    Flash Gordon McUsher's Avatar
    Join Date
    Mar 2001
    Location
    Krautland
    Posts
    1,560
    After talking to our java-guru (who got totally upset about the cast syntax of flash
    (on the other hand java geeks get upset about every flash syntax )) the solution
    to this problem is as simple as:

    Casting to Date doesn't work in Flash.
    My letters on the F1 key have faded, how are yours today?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center