473,408 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Strongly Typed ArrayLists

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

--
Dennis in Houston
Nov 21 '05 #1
5 1575
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

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:8F**********************************@microsof t.com... 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

--
Dennis in Houston

Nov 21 '05 #2
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.

"Jay B. Harlow [MVP - Outlook]" wrote:
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

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:8F**********************************@microsof t.com...
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

--
Dennis in Houston


Nov 21 '05 #3
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
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:09**********************************@microsof t.com... 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.

"Jay B. Harlow [MVP - Outlook]" wrote:
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

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:8F**********************************@microsof t.com...
> 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
>
> --
> Dennis in Houston


Nov 21 '05 #4
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.
--
Pete Wright
Author of ADO.NET Novice to Pro for Apress
www.petewright.org

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uH**************@TK2MSFTNGP10.phx.gbl...
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
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:09**********************************@microsof t.com...
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.

"Jay B. Harlow [MVP - Outlook]" wrote:
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

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:8F**********************************@microsof t.com...
> 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
>
> --
> Dennis in Houston


Nov 21 '05 #5
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
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bill Cohagan | last post by:
I'm trying to figure out the "best" way to implement a strongly typed ArrayList. Using inheritance is one approach. It has the advantage that I only have to write overrides for the Add method and...
1
by: Job Lot | last post by:
I am confused how strongly typed dataset is different from un-typed dataset. Is there any good link explaining pros and cons of both? Which one should be used preferably?
2
by: Mark | last post by:
Just wanted to confirm that my understanding of a strongly typed language is correct: 1. .NET is a strongly typed language because each variable / field must be declared a specific type (String,...
2
by: David | last post by:
I have been developing applications with Java for quite a while but I am new to .NET development. I am trying to learn the ".NET way" of creating Strongly Typed Objects from a database. The...
1
by: HardBap | last post by:
I've created a strongly typed DataSet (Customers.xsd) using the xsd.exe tool. I want to be able to access fields using ds.Customer.CompanyName. The problem is when I return this DataSet from a...
2
by: Ian Gore | last post by:
Hi, I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here... 1) Creating a strongly typed collection by inheriting CollectionBase. This is...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
1
by: Code Monkey | last post by:
Silly question maybe, but I've been doing the following for far too long now: public static DataTable myDataTable() { string sql = @"SELECT column1, column2, column3, column4 FROM...
4
by: Rachana | last post by:
Hi, I have understood Data Sets but what is meant by typed/untyped/ strongly typed datasets. Can any one explain me or suggest any site/ article, to get these concepts (and their ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.