views:

71

answers:

2

The dictionary use strict equals(===) for key comparison, how to change the comparison, so I can use my standard for comparison, for example, I have a class named Student:

    class Student{
     var id:int;
     var name:String;
     var age:int;
    //constructor
   Student(id:int,name:String,age:int){
         this.id = id;
         this.name = name;
         this.age = age;
      }
    }

I want Dictionary use id to compare if the two keys are equal, not use strict equal(===) to compare if the key is the same.

+3  A: 

Dictionary doesn't have any functionality like this. But unless I'm mistaken you'd get largely the same effect from a Dictionary (or Vector or Array or Object - whatever collection best suits your needs) full of Students indexed by id:

var studentsByID:Array = []; // <-- could be a vector, dict, custom collection class, etc..
// ...
studentsByID[someID] = new Student( someID, someName, someAge );
// ...
trace( "my ID:" + myID );
trace( "my Name:" + (studentsByID[myID] as Student).name );

Is there any reason not to do that?

fenomas
Essentially, what he wants is the ability to supply a Comparator for his Student, which he wants to use as a key. The answer is "you can't". Either use the ID itself as the key, as you suggest fenomas, or use another dictionary implementation like AS3CC's [SortedMap](http://www.sibirjak.com/projects/as3commons/apidoc/org/as3commons/collections/SortedMap.html).
Gunslinger47
@fenomas: Just out of curiosity, where are you getting that generic Dictionary from? Last I heard only Vector was generic.
Gunslinger47
@Gunslinger47: brain was on vacation there, I meant to say Vector. Thanks!
fenomas
That's not much better. It'll crash if you index the vector beyond the end of its current length. Just a regular Dictionary is fine. I edited your answer to demonstrate.
Gunslinger47
It will throw an RangeError (not crash) if you assign too high an index, and do lots of things if you do lots of other things. My suggestion is simply that the asker should consider a collection indexed by ID; what kind of collection would be best depends on information not provided. Also, please remember that the guideline for editing other people's answers is to "clarify the meaning without changing it".
fenomas
Sorry, fenomas. I thought I did just that since all I did was revert to your first revision and strip the `.<Student>`.
Gunslinger47
A: 

you can do next:

class Student{
 var id:int;
 var name:String;
 var age:int;
Student(id:int,name:String,age:int){
     this.id = id;
     this.name = name;
     this.age = age;
  }
    public function equals(s:Student):Boolean{
        return ((this.id==s.id)&&(this.name==s.name)&&(this.age==s.age));
    }
}

so later use it like:

var d:Dictionary=new Dictionary();
d["key"]=new Student();
//snippet
(d["key"] as Student).equals(new Student());

also if you want to overload as3 operators read next things:

http://stackoverflow.com/questions/446482/overload-operator-in-as3 http://livedocs.adobe.com/flex/3/langref/flash/utils/Proxy.html

Best Regards Eugene

Eugene
@Eugene. Thanks for your help. I confuse about the the operator [] in Dictionary. I mean when I use the "id" property of Student as the key, whether can I "filter" the same Student. such as : var d:Dictionary=new Dictionary(); s1:Student=new Student(1,"hello",20); d[s1]="test"; then I have another Student, s2:Student=new Student(1,"hello",20); the Dictionay can return the same value?
jason
the Dictionary will return something like Student@1223452 for first and Student@45332 for second, because of dynamic rules of AS, so it will be not the same values. But if you'll use equals function, it will return true.
Eugene
you can add comparator like this==s in equals func
Eugene
I don't see how this relates to the question - the dictionary won't use the equals function in deciding which object to return based on the key. You'd have to iterate the dictionary, which defeats the purpose of using it. Also, can you please expand on how using `nextProperty()` helps here?
Marty Pitt
@Marty Pitt sorry, what?
Eugene