473,408 Members | 1,772 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.

Accessing ArrayList index by a key not an index number

How do I go about accessing elements of an Arraylist using something other
than the index?

I have a class that manages a number of subclasses. Those subclasses are
stored in an arraylist. The subclass has a key property that is a string.
I would like to be able to call Class1.SubClassManager[string key] as
opposed to Class1.SubClassManager[int index].

I tried overriding ArrayList but then I wasn't sure what method to then
override or if there was a simpler method.

thanks

jack
Nov 16 '05 #1
5 6273
Jack,

In this case, you wouldn't use an ArrayList, you would use a Hashtable
(or if you wanted to create a strongly typed Hashtable, use the
DictionaryBase class).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jack Addington" <ja********@shaw.ca> wrote in message
news:Od**************@TK2MSFTNGP14.phx.gbl...
How do I go about accessing elements of an Arraylist using something other
than the index?

I have a class that manages a number of subclasses. Those subclasses are
stored in an arraylist. The subclass has a key property that is a string.
I would like to be able to call Class1.SubClassManager[string key] as
opposed to Class1.SubClassManager[int index].

I tried overriding ArrayList but then I wasn't sure what method to then
override or if there was a simpler method.

thanks

jack

Nov 16 '05 #2
You cannot. Make it a Hashtable if you need this functionality.

"Jack Addington" <ja********@shaw.ca> wrote in message
news:Od**************@TK2MSFTNGP14.phx.gbl...
How do I go about accessing elements of an Arraylist using something other
than the index?

I have a class that manages a number of subclasses. Those subclasses are
stored in an arraylist. The subclass has a key property that is a string.
I would like to be able to call Class1.SubClassManager[string key] as
opposed to Class1.SubClassManager[int index].

I tried overriding ArrayList but then I wasn't sure what method to then
override or if there was a simpler method.

thanks

jack

Nov 16 '05 #3
Everyone is pointing you at a Hashtable, but that is NOT the only solution. If
your collection is small, then you can do your own linear search through the
elements. If your collection is sorted, then you can use BinarySearch with
a custom comparer.

Your options are determined base on your data. If you have plenty of elements
then definitely go to a Hashtable, it will be faster.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Jack Addington" <ja********@shaw.ca> wrote in message
news:Od**************@TK2MSFTNGP14.phx.gbl...
How do I go about accessing elements of an Arraylist using something other
than the index?

I have a class that manages a number of subclasses. Those subclasses are
stored in an arraylist. The subclass has a key property that is a string. I
would like to be able to call Class1.SubClassManager[string key] as opposed
to Class1.SubClassManager[int index].

I tried overriding ArrayList but then I wasn't sure what method to then
override or if there was a simpler method.

thanks

jack

Nov 16 '05 #4
Or if you insist to use ArrayList's functionality then you can make your
own class that inherits System.Collections.CollectionBase class and
implement functionality for string indexer in it.

Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
Thanks all

"Jack Addington" <ja********@shaw.ca> wrote in message
news:Od**************@TK2MSFTNGP14.phx.gbl...
How do I go about accessing elements of an Arraylist using something other
than the index?

I have a class that manages a number of subclasses. Those subclasses are
stored in an arraylist. The subclass has a key property that is a string.
I would like to be able to call Class1.SubClassManager[string key] as
opposed to Class1.SubClassManager[int index].

I tried overriding ArrayList but then I wasn't sure what method to then
override or if there was a simpler method.

thanks

jack

Nov 16 '05 #6

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

Similar topics

4
by: Jazper | last post by:
hi i have a simple question about ArrayLists. Normaly ArrayLists will be accessed by index-number. ArrayList arr = new ArrayList(); arr.Add("test"); String str = arr.ToString(); Now, i'd...
7
by: Jerry S | last post by:
Hi I've got an arraylist of integers. I want to be able to remove an integer from the list using a method like this: public void ArrayListRemove(int indexnum) {...
7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
7
by: William Stacey [MVP] | last post by:
I can think of a couple ways to do this, but was wonder what the fastest way you can think of. Want to take any arraylist of > 1 element and make the first element the last, and all the rest of...
3
by: JJ | last post by:
Hi, I have created an Arraylist object from an Arraylist class. I added rows to the arraylist object and I need to find a particular record in my arraylist. How do I do this? Also if I was in...
8
by: Sek | last post by:
Folks, I have an ArrayList of integers. I have sorted the list already. Now, i want to find the index of the first element that is greater than a given number. How to accomplish this in C#?
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
3
by: Alex | last post by:
Hi all, I'm trying to create an arraylist of a user control class... I'm able to define the list and add objects (panels) to it, but I can access and of the panel properties using an index... ...
8
by: amazon | last post by:
I have a following structure that I am using with array list: Private Structure arrayliststruct Public Name As String Public value As String Public type As String End Structure and following...
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
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
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.