473,396 Members | 1,933 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,396 software developers and data experts.

ArrayList - Strongly Typed

sIs there anyway to Strongly type an arraylist. I have an arraylist where
object in the arraylist are of a type Structure, say "st" structure. I would
like to reference elements of the structure in the arraylist where "ar" is my
array list and "element 1 is an element in "st" like:

if ar(1).element1= xx then

so I don't have to use Ctype each time i want to get an element of the
arraylist structures.
--
Dennis in Houston
Nov 21 '05 #1
5 3169
Something like this:

Public Structure MyStruct
Public Item1 As Integer
Public Item2 As String
End Structure

Public Class StructList
Inherits ArrayList

Public Shadows Function Add(ByVal value As MyStruct) As Integer
Return MyBase.Add(value)
End Function

Default Public Shadows Property Item(ByVal index As Integer) As MyStruct
Get
Return CType(MyBase.Item(index), MyStruct)
End Get
Set(ByVal Value As MyStruct)
MyBase.Item(index) = Value
End Set
End Property
End Class

Private Sub Test( )
Dim myList As New StructList
Dim m_struct As MyStruct
m_struct.Item1 = 1
m_struct.Item2 = "test"

myList.Add(m_struct)
If myList(1).Item1 = 1 Then
Debug.WriteLine("found !")
End If
End Sub

This way you'll get a compile time error (with Option Strict On) both while
adding and while retrieving items that are not of type MyStruct.

hope that helps..
Imran.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
sIs there anyway to Strongly type an arraylist. I have an arraylist where
object in the arraylist are of a type Structure, say "st" structure. I
would
like to reference elements of the structure in the arraylist where "ar" is
my
array list and "element 1 is an element in "st" like:

if ar(1).element1= xx then

so I don't have to use Ctype each time i want to get an element of the
arraylist structures.
--
Dennis in Houston

Nov 21 '05 #2
On 2004-10-02, Dennis <De****@discussions.microsoft.com> wrote:
sIs there anyway to Strongly type an arraylist. I have an arraylist where
object in the arraylist are of a type Structure, say "st" structure. I would
like to reference elements of the structure in the arraylist where "ar" is my
array list and "element 1 is an element in "st" like:

if ar(1).element1= xx then

so I don't have to use Ctype each time i want to get an element of the
arraylist structures.


Two things... Don't put a structure into an arraylist (or any
collection acctually) if you can help it. This will cause a quite a bit
of overhead due to boxing. So, if it is at all possible, change your
structure to a class:

Class MyStruct
Public i As Integer
Public s As String
...
End Class

As for the strong typing... Yes, you can create a "strongly" typed
arraylist. The easiest way is to create a class that inherits from
System.Collections.CollectionBase.

--
Tom Shelton [MVP]
Nov 21 '05 #3
Well, the "REASON" you want this is so you do NOT have to CType when you get
an element...so create a class that inherits from ArrayList. When you
implement the methods andproperties (IE: Item), directcast/ctype there.

Mythran

"Imran Koradia" <no****@microsoft.com> wrote in message
news:OH**************@TK2MSFTNGP10.phx.gbl...
Something like this:

Public Structure MyStruct
Public Item1 As Integer
Public Item2 As String
End Structure

Public Class StructList
Inherits ArrayList

Public Shadows Function Add(ByVal value As MyStruct) As Integer
Return MyBase.Add(value)
End Function

Default Public Shadows Property Item(ByVal index As Integer) As MyStruct Get
Return CType(MyBase.Item(index), MyStruct)
End Get
Set(ByVal Value As MyStruct)
MyBase.Item(index) = Value
End Set
End Property
End Class

Private Sub Test( )
Dim myList As New StructList
Dim m_struct As MyStruct
m_struct.Item1 = 1
m_struct.Item2 = "test"

myList.Add(m_struct)
If myList(1).Item1 = 1 Then
Debug.WriteLine("found !")
End If
End Sub

This way you'll get a compile time error (with Option Strict On) both while adding and while retrieving items that are not of type MyStruct.

hope that helps..
Imran.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
sIs there anyway to Strongly type an arraylist. I have an arraylist where object in the arraylist are of a type Structure, say "st" structure. I
would
like to reference elements of the structure in the arraylist where "ar" is my
array list and "element 1 is an element in "st" like:

if ar(1).element1= xx then

so I don't have to use Ctype each time i want to get an element of the
arraylist structures.
--
Dennis in Houston


Nov 21 '05 #4
Also see "generics" in the upcoming VS 2005.
BobJ
"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:Or**************@TK2MSFTNGP09.phx.gbl...
On 2004-10-02, Dennis <De****@discussions.microsoft.com> wrote:
sIs there anyway to Strongly type an arraylist. I have an arraylist where object in the arraylist are of a type Structure, say "st" structure. I would like to reference elements of the structure in the arraylist where "ar" is my array list and "element 1 is an element in "st" like:

if ar(1).element1= xx then

so I don't have to use Ctype each time i want to get an element of the
arraylist structures.


Two things... Don't put a structure into an arraylist (or any
collection acctually) if you can help it. This will cause a quite a bit
of overhead due to boxing. So, if it is at all possible, change your
structure to a class:

Class MyStruct
Public i As Integer
Public s As String
...
End Class

As for the strong typing... Yes, you can create a "strongly" typed
arraylist. The easiest way is to create a class that inherits from
System.Collections.CollectionBase.

--
Tom Shelton [MVP]

Nov 21 '05 #5
Thanks for replies.

"Dennis" wrote:
sIs there anyway to Strongly type an arraylist. I have an arraylist where
object in the arraylist are of a type Structure, say "st" structure. I would
like to reference elements of the structure in the arraylist where "ar" is my
array list and "element 1 is an element in "st" like:

if ar(1).element1= xx then

so I don't have to use Ctype each time i want to get an element of the
arraylist structures.
--
Dennis in Houston

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...
7
by: WildHare | last post by:
If I have a class and I add it to an ArrayList and then want to access that class using using the index operator (e.g. ArrayList) the ArrayList returns a type "Object". I can cast the return to...
4
by: Bob Weiner | last post by:
What I want to be able to do is create an indexer that can index into an ArrayList filled with objects of my own type. I have the following class structure: ...
4
by: Dot net work | last post by:
I thought I'd be clever and create my own strongly typed array list, because I wanted to be sure that when an object was added to the arraylist, it would *only* be an object of a class that I had...
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...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
94
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. ...
44
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.