473,503 Members | 3,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

declaration of objects in a ArrayList , C#

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
object.
I want to declare the ArrayList so that it is known to the compiler which
type is stored in there so that I do not need the bulky syntax, and can use
intellisense.

Best regards, Jens
Aug 1 '05 #1
3 13933
Hello,
Try to use
public List<CAdresses> adresses = new List<CAdresses>();

But this only works in C# 2.0. In 1.1. you have to cast ArrayList members to
class you need.

Best regards,
Andrew

"Jens Stjärna" wrote:
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
object.
I want to declare the ArrayList so that it is known to the compiler which
type is stored in there so that I do not need the bulky syntax, and can use
intellisense.

Best regards, Jens

Aug 1 '05 #2
The Generics feature in c# 2.0 supports exactly this.

- Atul
Sky Software http://www.ssware.com/
Shell MegaPack ActiveX & .Net
Drop-In Windows Explorer-Like Shell Browsing UI for your apps.
"Jens Stjärna" <je**********@NOSPAMcallenberg.se> wrote in message
news:DB**********************************@microsof t.com...
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
object.
I want to declare the ArrayList so that it is known to the compiler which
type is stored in there so that I do not need the bulky syntax, and can
use
intellisense.

Best regards, Jens

Aug 1 '05 #3
Jens Stjärna wrote:
I want to declare the ArrayList so that it is known to the compiler which
type is stored in there so that I do not need the bulky syntax, and can use
intellisense.


In addition to what other have suggested about Generics in .NET 2.0, the
normal way to do this in .NET 1 was to derive your own collection class
from CollectionBase. Try looking at the MSDN docs for this class, I
think there's a template implementation there that you can use quickly
to create your own typed collection classes.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Aug 1 '05 #4

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

Similar topics

14
14263
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
10
1736
by: Jax | last post by:
I dont seem to fully comprehend references to objects yet. Lets say for example I do this; Customer c = new Customer(); Customer c1 = c; I understand that if I change c1, I also change c as...
5
1386
by: hazz | last post by:
If this makes sense, I would like to convert an arraylist of objects returned from foo() into an array of the objects themselves per; private Customer customers; private int idx = 0; public...
3
293
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...
3
6407
by: Arjen | last post by:
Hi, public class test { public ArrayList myList = new ArrayList(); } test myTest = new test(); myTest.myList.Add(<someobject>); This results in an error.
16
6395
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
2
4713
by: Rob Meade | last post by:
Dear all, I have a class which contains an arraylist populated with other objects, for example: PrescriptionQueue - containing multiple instances of Prescription I have the need on my web...
14
18724
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...
10
1876
by: netnet | last post by:
I have a collection of objects that I store in the session. Then I look in that collection to see if the collection.contains an object. it answers false. If I dont store that collection in the...
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7258
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,...
1
6970
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...
0
7441
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...
0
5558
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,...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1489
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 ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.