mskeel Posted April 19, 2005 Posted April 19, 2005 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. Quote
Diesel Posted April 19, 2005 Posted April 19, 2005 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. Quote
HJB417 Posted April 19, 2005 Posted April 19, 2005 you can use an utility such as codesmith to generate a source file for a strong typed collection. Quote
mskeel Posted April 20, 2005 Author Posted April 20, 2005 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. Quote
HJB417 Posted April 20, 2005 Posted April 20, 2005 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. Quote
Leaders snarfblam Posted April 21, 2005 Leaders Posted April 21, 2005 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.