ArrayList storing objects of only one type

B

bg_ie

Hi,

I currently use ArrayList to store a set of objects all of which have
the same type. The reason I use ArrayList is because it allows me Add
new items to the array. Should I be using something other than
ArrayList to serve this purpose? Its just that i have to cast my
objects each time I access them, as follows -

ArrayList xmlElements = new ArrayList();

xmlElements.Add(doc.CreateElement("Test"));
doc.AppendChild((XmlElement)xmlElements[0]);

Thanks,

Barry.
 
J

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

| I currently use ArrayList to store a set of objects all of which have
| the same type. The reason I use ArrayList is because it allows me Add
| new items to the array. Should I be using something other than
| ArrayList to serve this purpose? Its just that i have to cast my
| objects each time I access them, as follows -

IF you are using .NET 1.1, create a typesafe wrapper class; if you are using
..NET 2.0, use the generic List<T> class.

Joanna
 
J

Jon Skeet [C# MVP]

I currently use ArrayList to store a set of objects all of which have
the same type. The reason I use ArrayList is because it allows me Add
new items to the array. Should I be using something other than
ArrayList to serve this purpose? Its just that i have to cast my
objects each time I access them, as follows -

ArrayList xmlElements = new ArrayList();

xmlElements.Add(doc.CreateElement("Test"));
doc.AppendChild((XmlElement)xmlElements[0]);

Are you using .NET 1.1 or .NET 2.0 or higher? If you're using .NET 2.0
or higher, you should use List<T>, eg:

List<XmlElement> list = new List<XmlElement>();
xmlElements.Add(doc.CreateElement("Test"));
doc.AppendChild(list[0]);

If you're using 1.1, you'll have to create your own strongly-typed
collection - CollectionBase is a good base class for this, or there
are other implementations available on the web.

Jon
 
B

bg_ie

I currently use ArrayList to store a set of objects all of which have
the same type. The reason I use ArrayList is because it allows me Add
new items to the array. Should I be using something other than
ArrayList to serve this purpose? Its just that i have to cast my
objects each time I access them, as follows -
ArrayList xmlElements = new ArrayList();
xmlElements.Add(doc.CreateElement("Test"));
doc.AppendChild((XmlElement)xmlElements[0]);

Are you using .NET 1.1 or .NET 2.0 or higher? If you're using .NET 2.0
or higher, you should use List<T>, eg:

List<XmlElement> list = new List<XmlElement>();
xmlElements.Add(doc.CreateElement("Test"));
doc.AppendChild(list[0]);

If you're using 1.1, you'll have to create your own strongly-typed
collection - CollectionBase is a good base class for this, or there
are other implementations available on the web.

Jon

Sweet! I'm using 2.0. Thanks for your help.
 

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