Strongly Typed ArrayLists

G

Guest

NOTE: This is a COMMENT...not a QUESTION:

For the newbies, I've been messing around with strongly typed ArrayLists so
I don't have to type so many "DirectCast". The following works great where
"myClass" is the classs that each ArrayList Element will contain:

dim myarraylist as typedArrayList = new typedArrayList
Public Class typedArrayList
Inherits ArrayList
Default Public Shadows Property Item(ByVal Index As Integer) As myClass
Get
Return DirectCast(MyBase.Item(Index), myClass)
End Get
Set(ByVal Value As myclass)
MyBase.Item(Index) = Value
End Set
End Property
End Class

You can then reference any property in myclass in your code instead of using
direct cast to cast the arraylist element to myClass:

myPropertyValue = myarraylist(0).myProperty
 
J

Jay B. Harlow [MVP - Outlook]

Dennis,
Of course the danger of doing this is:
dim myarraylist as typedArrayList = new typedArrayList

Dim al As ArrayList = typedArrayList

al.Item is no longer typed! Which is a major danger of using Shadows! Two
major places where Shadows are useful are Version Control & overriding
Attributes on non Overridable methods. (Post if you would like more
information on these uses of Shadows).

I would recommend starting with CollectionBase to create a strongly typed
ArrayList. To absolutely ensure a strongly typed CollectionBase derived
class you may need to override CollectionBase.OnValidate & the other On*
methods of CollectionBase.

NOTE: This is a comment on your comment ;-)

Hope this helps
Jay
 
G

Guest

Understand your comments but I'm using this in my own program and I can avoid
the "dim al as arraylist = TypedArrayList. My method is quick, dirty and
works well for my internal programming.
 
J

Jay B. Harlow [MVP - Outlook]

Dennis,
My method is quick, dirty and
Is my point ;-)
works well for my internal programming.
As long as your programming is truly "internal" to you only. As soon as
someone inherits your "internal" code (such as a reader of this thread) and
uses the statement I showed....

Also I hope you understand why it can be dangerous. I would consider adding
a TODO on usages of Shadows such as yours, so anyone inheriting the code
realizes the risk...

Just a thought
Jay
 
P

Pete Wright

I'm with Jay on this as well. Even though your code is "internal" and only
being used by you, wouldn't you much rather grow as a developer and learn a
far better way to do what you are trying to do? After all, your code is
suffering the hit (all be it negligeable) of doing a Direct Cast every
single time you extract a value from the list, it's just hidden away out of
sight.
 
C

Cor Ligthert

Dennis,

In my opinion is the biggest problem that you want cout a cout use a list
where a collection is the properiate way to go.

What you now in the same opinion are trying to do is making from a list
class a collection class, where there are so many built in classes for that.

Just my thought,

Cor
 

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