473,391 Members | 1,496 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,391 software developers and data experts.

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 13919
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 Stjrna" <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
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
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
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
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
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
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
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
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
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
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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,...
0
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
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...

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.