473,769 Members | 6,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.CreateElem ent("Test"));
doc.AppendChild ((XmlElement)xm lElements[0]);

Thanks,

Barry.

Mar 29 '07 #1
3 1784
<bg***@yahoo.co ma écrit dans le message de news:
11************* *********@n76g2 00...legr oups.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.CreateElem ent("Test"));
doc.AppendChild ((XmlElement)xm lElements[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.CreateElem ent("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.com wrote:
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.CreateElem ent("Test"));
doc.AppendChild ((XmlElement)xm lElements[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.CreateElem ent("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
13945
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 ArrayList(); I store only objects from the class "CAdresses" in this ArrayList. Now, every time I use the objects in the ArrayList I have to type cast them into aCAdresses class type object since the compiler only knows it is a
0
7296
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 ago and it works fine. The question is if the ArrayList has a set of different objects of different type, every time different. We know what types should be involved, but we don't know what objects in what order and how many are used in the...
1
1977
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 repeater. But I always get an error message: "No default member found for type Stock." This is where the error came from: <ItemTemplate>
4
2823
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 that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been using an arraylist.add function to handle non-multidemional returns but was wondering if I'm better...
6
4322
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 or saved Arraylist, how is it done. It always seems to create a shallow copy i.e. by Reference. I even tried passing the original Arraylist to a function by Value and return a copy....after an arraylist.clear on the original it still clears the...
18
4745
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
18
3254
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 storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
14
18752
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 CType for casting but it is throwing exception.
4
12599
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 'System.Collections.ArrayList' to 'string'"
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.