ArrayList accessed by string

  • Thread starter Thread starter Jazper
  • Start date Start date
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
 
Jazper,

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



Mattias
 
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


.
 
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


.
 
Back
Top