Re: Find object inside arraylists by object attribute



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
>>
>>
>>


.



Relevant Pages

  • RE: Find object inside arraylists by object attribute
    ... you will need to override the Equals method inside your class such that it ... public override int GetHashCode() ... instead that you just use that in your arraylit instead: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: inheritance not working properly -- Help please!!
    ... when I type "public override", ... Equals, GetHashCode and ToString... ... method so I can override it in my inherited class? ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Equality vs Sameness
    ... Equals would not offer any benefit. ... >> from it in the future if the users haven't asked for that functionality. ... So you override Equals and GetHashCode even for forms, ... or wanted to compare two instances of a singleton? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: comparing objects
    ... I override the Equal method on SalesRegion ... As Arne already put it, the Contains method is using the Equals method, ... ArrayList salesRegions = ... private string s; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: override GetHashCode
    ... public override int GetHashCode() ... if two instances of SomeType are equal (cause _field equals), ... > public override int GetHashCode() ... >> the Hashtable uses the hash code of the key to determine the bucket that>> stores the key. ...
    (microsoft.public.dotnet.languages.csharp)