RE: Find object inside arraylists by object attribute
- From: "Mark R. Dawson" <MarkRDawson@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 27 Jul 2005 05:04:02 -0700
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:
- References:
- Prev by Date: Re: distinguish between server os and workstation
- Next by Date: RE: Unzip a file
- 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
|