473,671 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3179
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(valu e)
End Function

Default Public Shadows Property Item(ByVal index As Integer) As MyStruct
Get
Return CType(MyBase.It em(index), MyStruct)
End Get
Set(ByVal Value As MyStruct)
MyBase.Item(ind ex) = 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_st ruct)
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****@discuss ions.microsoft. com> wrote in message
news:91******** *************** ***********@mic rosoft.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****@discuss ions.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.Collecti ons.CollectionB ase.

--
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****@microso ft.com> wrote in message
news:OH******** ******@TK2MSFTN GP10.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(valu e)
End Function

Default Public Shadows Property Item(ByVal index As Integer) As MyStruct Get
Return CType(MyBase.It em(index), MyStruct)
End Get
Set(ByVal Value As MyStruct)
MyBase.Item(ind ex) = 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_st ruct)
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****@discuss ions.microsoft. com> wrote in message
news:91******** *************** ***********@mic rosoft.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*@YOUKNOWTHE DRILLmtogden.co m> wrote in message
news:Or******** ******@TK2MSFTN GP09.phx.gbl...
On 2004-10-02, Dennis <De****@discuss ions.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.Collecti ons.CollectionB ase.

--
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
5727
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 indexer. There are problems with this however. First the indexer "override" has a different return type. This doesn't cause a compile time error, but using the indexer doesn't seem to produce a properly typed expression. The Add override must...
7
1810
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 the correct type (my class) but that will lead to very convoluted calls to get embedded elements or to call methods. For example: I have a class called "Field"
4
1405
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: ---------------------------------------------- class PropertyList : ArrayList { public enum SortOrder { } public object this { // cannot figure this one t }
4
10278
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 made. But after I got it all working, I realised that the inheritance mechanism also allows access to the inherited Add methods, and these allow you to add objects of any type! I don't seem to have achieved what I tried to do.
20
5970
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 arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim mylist as new FrameList mylist.Add(new FrameStructure) mylist(0).first = "blabla..." mylist(0).second...
18
4730
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
94
5660
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. Thanks.
44
39157
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 List<is far better. So, why do people use ArrayList so often? Am I missing somehing? What's the difference between them? Zytan
0
8473
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8597
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2808
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.