declaration of objects in a ArrayList , C#

G

Guest

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
 
G

Guest

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
 
O

Oliver Sturm

Jens said:
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
 

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