473,382 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ArrayList storing objects of only one type

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.

Mar 29 '07 #1
3 1775
<bg***@yahoo.coma écrit dans le message de news:
11**********************@n76g2000hsh.googlegroups. com...

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

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 29 '07 #2
On Mar 29, 8:02 am, b...@yahoo.com wrote:
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<XmlElementlist = 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

Mar 29 '07 #3
On 29 Mar, 09:12, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Mar 29, 8:02 am, b...@yahoo.com wrote:
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<XmlElementlist = 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.

Mar 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jens Stjärna | last post by:
Hi. I have a question regarding the ArrayList. In my code I use the arraylist to store objects of certain class. I do not mix object types in the same ArrayList. public ArrayList adresses = new...
0
by: Just D. | last post by:
There is an interesting article - http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=542&printer=t It shows how to serialize the ArrayList of identical objects. I did it a year...
1
by: wrytat | last post by:
I have an arraylist storing objects that I've defined my own. The object is called Stock. And each stock has attributes like Comp, PartNo, Model, and etc. I want to bind this arraylist with a...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
6
by: Desmond Cassidy | last post by:
Hi, I'm sure this has been asked several times before but I'll risk it ;-) If I wish to save an Arraylist to another Arraylist and work on te original without affecting the contents of the new...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
14
by: budy_ludy | last post by:
Hi All, I am new to vb .net, I have an ArrayList and i store class objects in it, and later i want to retrieve each ArrayList items and type cast to the class, How can it be done ? I used...
4
by: =?Utf-8?B?YmFzdWxhc3o=?= | last post by:
Hi; I want to store a datatable (or an arraylist) as a session variable but when I try; Session = al_RecNo; I get an error that; "Cannot implicitly convert type...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.