4

In java, you can get a unique string for an object.

How can you do this in as3?

Community
  • 1
  • 1
jedierikb
  • 12,752
  • 22
  • 95
  • 166
  • Just to be clear, Java's hashCode() isn't exactly "unique"... hash collisions can still cause distinct objects to return the same hashCode() value. – Zach Scrivena Jul 20 '09 at 04:19
  • this is a helpful page ( http://www.morearty.com/blog/2008/07/28/actionscripts-different-ways-to-convert-an-object-to-a-string/ ), but the results are not unique... in fact, most are exactly the same for a given type of object. – jedierikb Jul 20 '09 at 04:57

3 Answers3

4

you can use this, to get a unique uint ... if you want to, convert it to a string ... :-P

package {
    import flash.utils.Dictionary;
    public class ObjectUIDUtil {
        private static var _uids:Dictionary = new Dictionary(true);
        private static var _cter:uint = 1;
        public static function getUID(obj:Object):uint {
            var ret:uint = _uids[obj];
            return (ret == 0) ? (_uids[obj] = _cter++) : ret;
        }
    }
}

please note, that this maybe is not even necessary, since flash.utils.Dictionary allows using objects as keys directly ...

greetz

back2dos

back2dos
  • 15,588
  • 34
  • 50
  • I'm curious, what's the advantage of using `Dictionary` over a normal Object? (I honestly don't know and am interested). – Marty Jun 06 '12 at 06:47
  • 1
    The problem here is, that if you use any value as a key for an `Object` the value is converted to a string and objects of the same type have the same string representation by default, so you'd get the same id for every instance of the same class. `Dictionary` leaves the value largely unmodified, although it will treat the `String` `"1"` and the `int` `1` as the same key, but it will distinguish non-primitive objects by their identity. – back2dos Jun 06 '12 at 11:01
3

in the same vain as the responses on the java thread, the is a unique ID generator as part of the flex SDK. This is found under mx.utils.UIDUtil, it works fairly simply.

var ID:String  = UIDUtil.createUID();

Hope this helps.

enzuguri
  • 818
  • 5
  • 10
0

You can try using a third party hashing function (such as md5 or sha1). The hashcode for objects in Java (incidentally C#) is generated by a hashing function as well. Here's one I found on Google

Hope this helps.

Extrakun
  • 19,057
  • 21
  • 82
  • 129