469,282 Members | 1,948 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

ArrayList OF Objects

Hi All,
i do have an array list of objects

eg: ArrayList al=new ArrayList();
al.Add(new MyOb(1);
al.Add(new MyOb(2);
from this i need to access a particular object values , how do i do
it??

eg:
From the Arraylist how do get object with variable 2
Thanks in Advance

thomson

Jul 19 '07 #1
4 6213
"thomson" <sa**********@yahoo.comha scritto nel messaggio
news:11*********************@z24g2000prh.googlegro ups.com...
Hi All,
i do have an array list of objects

eg: ArrayList al=new ArrayList();
al.Add(new MyOb(1);
al.Add(new MyOb(2);
from this i need to access a particular object values , how do i do
it??

eg:
From the Arraylist how do get object with variable 2
Use a List<MyObinstead of an ArrayList.
--
Help The New .Net Site! http://www.devbox4.net
Jul 19 '07 #2
Hi thomson,

Base method is foreach(object in al) { if (object.Property == 2) ... } if
you are on .NET 1.1
or use List<objectlist and list.Find() on .NET 2.0.

It's for values. For just one value as key it's better to consider using
Hashtable(1.x)/Dictionary<>(2.0)

Regards, Alex
[TechBlog] http://devkids.blogspot.com
Jul 19 '07 #3
On Thu, 19 Jul 2007 14:38:05 +0200, AlexS <sa***********@SPAMrogers.comPLEASEwrote:
You added second value at index 1 (0-based array). So you can get it back as

object o = al[1];

If you need to check type, use GetType method

if (al[1].GetType() == typeof(MyObj)) { ... }
Or if you just want to know if you can treat the object as a certain type

if(al[1] is MyObj) { ... }

--
Happy coding!
Morten Wennevik [C# MVP]
Jul 19 '07 #4
On Thu, 19 Jul 2007 14:09:43 +0200, thomson <sa**********@yahoo.comwrote:
Hi All,
i do have an array list of objects

eg: ArrayList al=new ArrayList();
al.Add(new MyOb(1);
al.Add(new MyOb(2);
from this i need to access a particular object values , how do i do
it??

eg:
From the Arraylist how do get object with variable 2
Thanks in Advance

thomson

MyOb obj = (MyOb)al[1];

Use [] to access a particular item in a list given its index. al[1] will return your item, but as an Object reference so you need to cast it toMyOb.

Anyhow, if you are going to use a single type of items in a list, use List instead (.Net 2.0) as you can then access the items without having toworry about types. You'll get a compiler or runtime error if you put anything else but the correct type in such a list.

List<MyObal = new List<MyOb>();
al.Add(new MyOb());
al.Add(new MyOb());

MyOb obj = al[1];

--
Happy coding!
Morten Wennevik [C# MVP]
Jul 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jens Stjärna | last post: by
9 posts views Thread by TT ( Tom Tempelaere ) | last post: by
4 posts views Thread by Hans De Schrijver | last post: by
reply views Thread by Just D. | last post: by
19 posts views Thread by Derek Martin | last post: by
6 posts views Thread by GrandpaB | last post: by
14 posts views Thread by Kym | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.