ArrayList accessed by string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jazper

    #1

    ArrayList accessed by string

    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
  • Mattias Sjögren

    #2
    Re: ArrayList accessed by string

    Jazper,

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



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Jazper

      #3
      Re: ArrayList accessed by string

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


      [color=blue]
      >-----Original Message-----
      >ArrayList is not designed for that. You are better off[/color]
      using HashTable,[color=blue]
      >which is an equivavlent to a collection in VB.
      >
      >"Jazper" <j.a.z@bluewin. ch> wrote in message
      >news:0d6501c35 c38$29a3f200$a6 01280a@phx.gbl. ..[color=green]
      >> 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[/color][/color]
      have[color=blue][color=green]
      >> to code this behaviour myself?
      >>
      >> thanx for every little answer!
      >> regards, Jazper[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Jazper

        #4
        Re: ArrayList accessed by string

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


        [color=blue]
        >-----Original Message-----
        >Jazper,
        >
        >Use a Hashtable or some other kind of dictionary instead [/color]
        of an[color=blue]
        >ArrayList.
        >
        >
        >
        >Mattias
        >
        >--
        >Mattias Sjögren [MVP] mattias @ mvps.org
        >http://www.msjogren.net/dotnet/
        >Please reply only to the newsgroup.
        >.
        >[/color]

        Comment

        • john farrow

          #5
          Re: ArrayList accessed by string

          You might want to check out
          System.Collecti ons.Specialized .NameValueColle ction which is specifically a
          type safe string to string map.

          Regards

          John

          "Jazper" <j.a.z@bluewin. ch> wrote in message
          news:a16801c35c 4d$12ec2950$a00 1280a@phx.gbl.. .[color=blue]
          > Oh, cool. Didn't know this class til not.
          > Thank you.
          >
          >
          >[color=green]
          > >-----Original Message-----
          > >ArrayList is not designed for that. You are better off[/color]
          > using HashTable,[color=green]
          > >which is an equivavlent to a collection in VB.
          > >
          > >"Jazper" <j.a.z@bluewin. ch> wrote in message
          > >news:0d6501c35 c38$29a3f200$a6 01280a@phx.gbl. ..[color=darkred]
          > >> 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[/color][/color]
          > have[color=green][color=darkred]
          > >> to code this behaviour myself?
          > >>
          > >> thanx for every little answer!
          > >> regards, Jazper[/color]
          > >
          > >
          > >.
          > >[/color][/color]


          Comment

          Working...