ArrayList - Strongly Typed

G

Guest

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.
 
I

Imran Koradia

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.
 
T

Tom Shelton

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.
 
M

Mythran

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
 
B

BobJ

Also see "generics" in the upcoming VS 2005.
BobJ
Tom Shelton said:
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.
 

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