Re: Find object inside arraylists by object attribute
- From: "Arjen" <boah123@xxxxxxxxxxx>
- Date: Wed, 27 Jul 2005 15:03:53 +0200
If I want to use "indexOf" then it uses the "public override bool Equals"
method?
Thanks!
"Mark R. Dawson" <MarkRDawson@xxxxxxxxxxxxxxxxxxxxxxxxx> schreef in bericht
news:9038EBA0-150B-4837-824A-2532A0934390@xxxxxxxxxxxxxxxx
> Hi Arjen,
> you will need to override the Equals method inside your class such that
> it
> checks for equality based on the attribute you desire, such as name.
>
> For example, i fyou want to check for an item in the arraylist based on
> the
> name of a person:
>
> class clsPerson
> {
> private string m_strName;
>
> public clsPerson(string strName)
> {
> m_strName = strName;
> }
>
> public string Name
> {
> get
> {
> return m_strName;
> }
> }
>
> //override this to check how two objects are equal
> public override bool Equals(Object o)
> {
> return (clsPerson)o.Name == this.m_strName;
> }
>
> //whenever you override equals you should override GetHashCode too
> //because some classes will call GetHashCode first before Equals as a
> quick
> //check for equality, i.e. if the HashCodes match then check equals
> otherwise
> //keep moving
>
> //The ArrayList.IndexOf method does not call GetHashCode
> public override int GetHashCode()
> {
> return valid hashcode for your object
> }
> }
>
>
> Then:
> ArrayList lst = new arrayList();
> clsPerson p1 = new clsPerson("mark");
> clsPerson p2 = new clsPerson("bob");
>
> //this i sthe person we are searching for:
> clsPerson p3 = new clsPerson("bob");
>
> //add mark and bob
> lst.Add(p1);
> lst.Add(p2);
>
> //now does mark exists in the list:
> lst.IndexOf(p3) -> should return 1, which is the index location of bob in
> the list
>
>
>
> Alernatively if you don't want to override the Equals on your object
> clsPerson, because that equality test is true for this arraylist
> situation,
> but it may not be for other situations, you could do it on some proxy
> object
> instead that you just use that in your arraylit instead:
>
> i.e.
>
> class PersonSearchProxy
> {
> private clsPerson m_person;
>
> public PersonSearchProxy(clsPerson p)
> {
> m_person = p;
> }
>
> public clsPerson Value
> {
> get
> {
> return m_person;
> }
> }
>
> //override the equals and gethashcode of this class
> public override bool Equals(Object o)
> {
> return (clsPerson)o.Name == this.m_person.Name;
> }
>
> public override int GetHashCode()
> {
> //return valid hashcode
> }
> }
>
> //now add instances of PersonSearchProxy to the arrylist.
>
>
> Hope that helps:
> Mark R. Dawson
>
> "Arjen" wrote:
>
>> Hi,
>>
>> I have a class with some attributes. For example class person with name
>> as
>> attribute.
>> I have add multiple persons in an arraylist.
>>
>> Now I need a function to get/find a person by the name attribute. This
>> function returns the index or the object.
>>
>> I can do this with a foreach statement. I think there are better
>> solutions,
>> like the binarysearch function inside the arraylist. The problem is that
>> it
>> can only search persons by objectkey. I need to search by the attribute
>> of
>> that object.
>>
>> Can somebody tell me how to do this with a good example?
>> I found things (google) about IComparer... but I don't understand these
>> sample's and I don't get is to work.
>>
>> After that I also want to sort the persons inside the arraylist by an
>> attribute.
>> How does this work?
>>
>> Many thanks!!!
>> Arjen
>>
>>
>>
.
- Follow-Ups:
- Re: Find object inside arraylists by object attribute
- From: Jon Skeet [C# MVP]
- Re: Find object inside arraylists by object attribute
- References:
- Find object inside arraylists by object attribute
- From: Arjen
- RE: Find object inside arraylists by object attribute
- From: Mark R. Dawson
- Find object inside arraylists by object attribute
- Prev by Date: Re: Generics: Inheritance and Interface questions
- Next by Date: Re: Thread
- Previous by thread: RE: Find object inside arraylists by object attribute
- Next by thread: Re: Find object inside arraylists by object attribute
- Index(es):
Relevant Pages
|