ArrayList accessed by string

J

Jazper

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[0].ToString();

Now, i'd like to access not by index-number. i'd like to
access by index-string like...
ArrayList arr = new ArrayList();
arr["myindex"] = "test";
String str = arr["myindex"].ToString();

is this possible? is ArrayList designed for things like
that or do i have to use another class? or do i even have
to code this behaviour myself?

thanx for every little answer!
regards, Jazper
 
M

Mattias Sjögren

Jazper,

Use a Hashtable or some other kind of dictionary instead of an
ArrayList.



Mattias
 
J

Jazper

Oh, cool. Didn't know this class til not.
Thank you.


-----Original Message-----
ArrayList is not designed for that. You are better off using HashTable,
which is an equivavlent to a collection in VB.

Jazper said:
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[0].ToString();

Now, i'd like to access not by index-number. i'd like to
access by index-string like...
ArrayList arr = new ArrayList();
arr["myindex"] = "test";
String str = arr["myindex"].ToString();

is this possible? is ArrayList designed for things like
that or do i have to use another class? or do i even have
to code this behaviour myself?

thanx for every little answer!
regards, Jazper


.
 
J

john farrow

You might want to check out
System.Collections.Specialized.NameValueCollection which is specifically a
type safe string to string map.

Regards

John

Jazper said:
Oh, cool. Didn't know this class til not.
Thank you.


-----Original Message-----
ArrayList is not designed for that. You are better off using HashTable,
which is an equivavlent to a collection in VB.

Jazper said:
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[0].ToString();

Now, i'd like to access not by index-number. i'd like to
access by index-string like...
ArrayList arr = new ArrayList();
arr["myindex"] = "test";
String str = arr["myindex"].ToString();

is this possible? is ArrayList designed for things like
that or do i have to use another class? or do i even have
to code this behaviour myself?

thanx for every little answer!
regards, Jazper


.
 

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