PC Review


Reply
Thread Tools Rate Thread

ArrayList and indexOf

 
 
tony
Guest
Posts: n/a
 
      23rd Aug 2006
Hello!

Assume I have created several object of a class called Test and then used
add all these Test object to an ArrayList.

Assume also that in this class Test I have a field with a property called
name.
Assume I add a Test object where the name field is equal to "Nisse"

Now to my question:
Is it possible to find if there exist an object in the ArrayList with the
name="Nisse"

//Tony


 
Reply With Quote
 
 
 
 
Barry Kelly
Guest
Posts: n/a
 
      23rd Aug 2006
tony wrote:

> Hello!
>
> Assume I have created several object of a class called Test and then used
> add all these Test object to an ArrayList.
>
> Assume also that in this class Test I have a field with a property called
> name.
> Assume I add a Test object where the name field is equal to "Nisse"
>
> Now to my question:
> Is it possible to find if there exist an object in the ArrayList with the
> name="Nisse"


Yes - write a loop and check each object.

foreach (Test t in list)
if (t.Name == "Nisse")
// found

If you used List<T> instead of ArrayList (and you're using C# 2.0 on
..NET 2.0), you could do it with a predicate passed to List<T>.FindAll()
/ List<T>.FindIndex(), but it wouldn't really be any less code.

If you were using the LINQ preview of C# 3.0, you could write:

bool found = list.FindIndex(e => e.Name == "Nisse") != -1;

.... but that's not for production use yet.

-- Barry

--
http://barrkel.blogspot.com/
 
Reply With Quote
 
Stoitcho Goutsev \(100\)
Guest
Posts: n/a
 
      23rd Aug 2006
Tony,

If the ordering of elements is not important to you you can use Hastable
instead. Hastables registers elements under some key value. You can use
latter on the key to extract the element.

Hashtable ht = new Hashtable()

ht[test.name] = test;

ht["Nisse"] will return the object which proeprty name was Nisse.

Keep in mind that keys cannot duplicate as well as if you change the *name*
property after the object has been added to the hashtable trying to retrieve
the item using the new name won't work. The element needs to be re-added
upon changing the name.


--
HTH
Stoitcho Goutsev (100)


"tony" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello!
>
> Assume I have created several object of a class called Test and then used
> add all these Test object to an ArrayList.
>
> Assume also that in this class Test I have a field with a property called
> name.
> Assume I add a Test object where the name field is equal to "Nisse"
>
> Now to my question:
> Is it possible to find if there exist an object in the ArrayList with the
> name="Nisse"
>
> //Tony
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arraylist IndexOf Structure ray@Mississauga.Canada Microsoft ASP .NET 0 18th Feb 2010 03:15 PM
ArrayList.IndexOf method Qubeczek Microsoft Dot NET Compact Framework 0 17th Jun 2006 01:09 PM
ArrayList.IndexOf(Object obj) functionality changed in .Net 2.0 =?Utf-8?B?U2FpbEJvZmZpbg==?= Microsoft Dot NET Framework 2 6th Apr 2006 07:14 AM
Case insensitive Arraylist.indexof search JohnR Microsoft VB .NET 10 12th Oct 2005 03:06 PM
ArrayList.indexOf(self-defined class) paulyip Microsoft C# .NET 1 21st Jan 2005 06:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 PM.