Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating an ArrayList of Objects c#

0 views
Skip to first unread message

Netmonster

unread,
Dec 9, 2004, 11:23:51 AM12/9/04
to
Hello,

Does any one have an example of how to create an ArrayList of objects?
i.e. ArrayList of ArrayLists or an ArrayList of Hashtables?
Thanks in advance

KC

Nicholas Paldino [.NET/C# MVP]

unread,
Dec 9, 2004, 11:48:43 AM12/9/04
to
KC,

It's like adding any other item to an ArrayList, and then retrieving
that.

For example, if you wanted to add a Hashtable to an ArrayList, then you
would do this:

// An array list.
ArrayList arrayList = new ArrayList();

// Add 100 hashtables.
for (int index = 0; index < 100; index++)
{
// Add a hashtable.
arrayList.Add(new Hashtable());
}

Now if you want to use one of the hashtables, you would have to cast the
item that the array list returns:

// Get the first hashtable.
Hashtable hashTable = (Hashtable) arrayList[0];

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Netmonster" <netmon...@yahoo.com> wrote in message
news:1102609431....@f14g2000cwb.googlegroups.com...

Netmonster

unread,
Dec 9, 2004, 12:56:57 PM12/9/04
to
What if you don't know the number of hashtables you want to add?

Syanide

unread,
Dec 9, 2004, 1:06:31 PM12/9/04
to
arraylist is dynamic

ie..

ArrayList x1 = new ArrayList();

x1.Add (your object here)
x1.Add( another object here)
etc....


"Netmonster" <netmon...@yahoo.com> wrote in message

news:1102615017.5...@z14g2000cwz.googlegroups.com...

Nicholas Paldino [.NET/C# MVP]

unread,
Dec 9, 2004, 1:07:08 PM12/9/04
to
Netmonster,

The array list is self-allocating. So you just keep calling Add when
you need to in order to add a new Hashtable. You don't have to know how
many you need beforehand.


--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Netmonster" <netmon...@yahoo.com> wrote in message

news:1102615017.5...@z14g2000cwz.googlegroups.com...

Netmonster

unread,
Dec 9, 2004, 3:24:40 PM12/9/04
to
Thanks.... It is working now


KC

0 new messages