Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

What are some of the techniques that you use to make your collections type safe? In other words, make it so that an ArrayList will only take an object of type CustomObject (insert some type here...string, int, employee, datafile, etc..)

 

This article outlines three differnet techniques: inheriting from an existing collection class (e.g. ArrayList), encapsulation, and inheriting from CollectionBase and DictionaryBase.

 

I know what Plausibly Damp would do but it still seems like a lot of work, esspecially if you have more than one collection you'd like to strongly type. And why did you choose that technique over others? It also seems this will be a moot point when whidby is released someday.

 

But for now, is there another way to strongly type collections? Are there any major advantages and disatavantages to these various techniques other than the few outlined in the above article?

 

Any advice and opinions would be greatly apprciated.

Posted
The problem I've had with inheriting (from ArrayList) is that the interface still supports adding object(s) to the collection. You can shadow the method, but it will still be available. I've used encapsulation but if you don't need a lot of the methods from the collection class I would inherit from CollectionBase.
Posted
The problem I've had with inheriting (from ArrayList) is that the interface still supports adding object(s) to the collection.

The interesting thing is that I don't believe this is true for VB (shadows keyword). It is definately the case for C#, though. Sort of an interesting feature. I wonder why they allowed you to completely shadow a method in VB but you can only overload (or override but that doesn't help here) in C#?

 

Would it still be considered good practice to inherit from ArrayList, say, in VB with the shadowing functionality? With collection base will it still accept objects and the specific type?

 

HJB417 -- I'll be sure to check that out. It sounds like it would be just the trick to make strong typing bearable.

 

CodeSmith (this is the right one, yes?)

Intersting MSDN Article on codesmith

 

Thank you for you help.

Posted

it should be the same for VB (.NET) because of the nature of the .NET runtime --> Because the arraylist implements the IList interface. One can simply cast an object to an IList and invoke the methods an IList is required to implement.

 

Yuu have the right homepage for code smith. It's not limited to created typesafe collections as you probably found out. I don't know the full extent of it's capabilities but I know one of the things it does is generate code for type safe collections.

  • Leaders
Posted

Although you can shadow functions in VB even if they have different signatures or return types, since you aren't overriding them, they are still accessible. If you inherit ArrayList and shadow the methods, you could still do either of the following to access the original methods that allow adding objects: DirectCast(MyInheritedClass, ArrayList).Add(MyObject) or DirectCast(MyInheritedClass, IList).Add(MyObject).

 

I personally stay away from shadowing as much as possible. It makes it very easy to accidentally call the wrong function, and sometimes difficult to dubug. When I've needed strongly typed collections in the past, I've actually made my own classes that manage memory in the same manner as the ArrayList, and I implemented IList and IEnumerable myself.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...