By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,027 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,027 IT Pros & Developers. It's quick & easy.

ArrayList - Strongly Typed

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.