Creating an ArrayList of Objects c#

N

Netmonster

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
S

Syanide

arraylist is dynamic

ie..

ArrayList x1 = new ArrayList();

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

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top