By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,276 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,276 IT Pros & Developers. It's quick & easy.

Find object inside arraylists by object attribute

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Nov 17 '05 #2

P: n/a
>I can do this with a foreach statement. I think there are better solutions,
like the binarysearch function inside the arraylist.


BinarySearch can only be used if the list is sorted by Name. Otherwise
a linear search (with foreach) is as good as it gets when using an
ArrayList. There are of course other data structures you can use if
you want fast lookup by name, for example a Hashtable keyed by the
name.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3

P: n/a
If I want to use "indexOf" then it uses the "public override bool Equals"
method?

Thanks!

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> schreef in bericht
news:90**********************************@microsof t.com...
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

Nov 17 '05 #4

P: n/a
Arjen <bo*****@hotmail.com> wrote:
If I want to use "indexOf" then it uses the "public override bool Equals"
method?


Yes - as per the docs: "This method determines equality by calling
Object.Equals."

It's a shame you can't pass in an IComparer, to allow you to match by
varying criteria.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.