469,353 Members | 2,066 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

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 13758
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by vince | last post: by
10 posts views Thread by Jax | last post: by
3 posts views Thread by Jens Stjärna | last post: by
3 posts views Thread by Arjen | last post: by
2 posts views Thread by Rob Meade | last post: by
10 posts views Thread by netnet | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.