Generic collection question

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

    #1

    Generic collection question

    I am doing a Generic.Diction ary(of string,myobject )

    I need to access the item by key, but also by index. Is this the wrong thing
    to use? Should I be using something different.

    In other words sometimes I just need item(0), the first item added to the
    list, but other times by key.

    TIA,

    Shane


  • Theo Verweij

    #2
    Re: Generic collection question

    To access the keys by index, use the following code:

    Dim KeyArray(YourDi ctionary.Keys.C ount) as string
    YourDictionary. Keys.CopyTo(Key Array)

    ValueByIndex = YourDictionary( KeyArray(Index) )

    I am doing a Generic.Diction ary(of string,myobject )
    >
    I need to access the item by key, but also by index. Is this the wrong thing
    to use? Should I be using something different.
    >
    In other words sometimes I just need item(0), the first item added to the
    list, but other times by key.
    >
    TIA,
    >
    Shane
    >
    >

    Comment

    • SStory

      #3
      Re: Generic collection question

      Well,

      I was reading the documentation which says there is no way to guarantee the
      order.
      I need item 0.

      I am now trying to build my own generic class to do this.

      Thanks,

      Shane

      "Theo Verweij" <tverweij@xs4al l.nlwrote in message
      news:%231krgzHy GHA.3440@TK2MSF TNGP06.phx.gbl. ..
      To access the keys by index, use the following code:
      >
      Dim KeyArray(YourDi ctionary.Keys.C ount) as string
      YourDictionary. Keys.CopyTo(Key Array)
      >
      ValueByIndex = YourDictionary( KeyArray(Index) )
      >
      >
      >I am doing a Generic.Diction ary(of string,myobject )
      >>
      >I need to access the item by key, but also by index. Is this the wrong
      >thing to use? Should I be using something different.
      >>
      >In other words sometimes I just need item(0), the first item added to the
      >list, but other times by key.
      >>
      >TIA,
      >>
      >Shane

      Comment

      • Kerry Moorman

        #4
        Re: Generic collection question

        Shane,

        You might be able to build off of NameObjectColle ctionBase.

        Kerry Moorman


        "SStory" wrote:
        Well,
        >
        I was reading the documentation which says there is no way to guarantee the
        order.
        I need item 0.
        >
        I am now trying to build my own generic class to do this.
        >
        Thanks,
        >
        Shane
        >
        "Theo Verweij" <tverweij@xs4al l.nlwrote in message
        news:%231krgzHy GHA.3440@TK2MSF TNGP06.phx.gbl. ..
        To access the keys by index, use the following code:

        Dim KeyArray(YourDi ctionary.Keys.C ount) as string
        YourDictionary. Keys.CopyTo(Key Array)

        ValueByIndex = YourDictionary( KeyArray(Index) )

        I am doing a Generic.Diction ary(of string,myobject )
        >
        I need to access the item by key, but also by index. Is this the wrong
        thing to use? Should I be using something different.
        >
        In other words sometimes I just need item(0), the first item added to the
        list, but other times by key.
        >
        TIA,
        >
        Shane
        >
        >
        >

        Comment

        • Theo Verweij

          #5
          Re: Generic collection question

          I just runned some tests, and it seems that the order stays in the order
          you added the items, as long as you don't use the remove method.



          SStory wrote:
          Well,
          >
          I was reading the documentation which says there is no way to guarantee the
          order.
          I need item 0.
          >
          I am now trying to build my own generic class to do this.
          >
          Thanks,
          >
          Shane
          >
          "Theo Verweij" <tverweij@xs4al l.nlwrote in message
          news:%231krgzHy GHA.3440@TK2MSF TNGP06.phx.gbl. ..
          >To access the keys by index, use the following code:
          >>
          >Dim KeyArray(YourDi ctionary.Keys.C ount) as string
          >YourDictionary .Keys.CopyTo(Ke yArray)
          >>
          >ValueByIndex = YourDictionary( KeyArray(Index) )
          >>
          >>
          >>I am doing a Generic.Diction ary(of string,myobject )
          >>>
          >>I need to access the item by key, but also by index. Is this the wrong
          >>thing to use? Should I be using something different.
          >>>
          >>In other words sometimes I just need item(0), the first item added to the
          >>list, but other times by key.
          >>>
          >>TIA,
          >>>
          >>Shane
          >
          >

          Comment

          • SStory

            #6
            Re: Generic collection question

            That's what I did and finally got it going.

            For any who read this.. BE WARE that For Each doesn't work well with it
            unless you override the GetEnumerator and return the values.GetEnume rator

            This will make it work as you would expect. Otherwise you get an
            enumeration of the keys.

            Thanks to all!

            Shane

            "Kerry Moorman" <KerryMoorman@d iscussions.micr osoft.comwrote in message
            news:A4E28518-C279-41FD-8C03-B48217FFA742@mi crosoft.com...
            Shane,
            >
            You might be able to build off of NameObjectColle ctionBase.
            >
            Kerry Moorman
            >
            >
            "SStory" wrote:
            >
            >Well,
            >>
            >I was reading the documentation which says there is no way to guarantee
            >the
            >order.
            >I need item 0.
            >>
            >I am now trying to build my own generic class to do this.
            >>
            >Thanks,
            >>
            >Shane
            >>
            >"Theo Verweij" <tverweij@xs4al l.nlwrote in message
            >news:%231krgzH yGHA.3440@TK2MS FTNGP06.phx.gbl ...
            To access the keys by index, use the following code:
            >
            Dim KeyArray(YourDi ctionary.Keys.C ount) as string
            YourDictionary. Keys.CopyTo(Key Array)
            >
            ValueByIndex = YourDictionary( KeyArray(Index) )
            >
            >
            >I am doing a Generic.Diction ary(of string,myobject )
            >>
            >I need to access the item by key, but also by index. Is this the wrong
            >thing to use? Should I be using something different.
            >>
            >In other words sometimes I just need item(0), the first item added to
            >the
            >list, but other times by key.
            >>
            >TIA,
            >>
            >Shane
            >>
            >>
            >>

            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Generic collection question

              SStory,
              If I need a generic collection with access by key & index I will normally
              use a System.Collecti ons.ObjectModel .KeyedCollectio n(Of TKey, TItem).
              Especially when used as a base class.

              --
              Hope this helps
              Jay B. Harlow [MVP - Outlook]
              ..NET Application Architect, Enthusiast, & Evangelist
              T.S. Bradley - http://www.tsbradley.net


              "SStory" <nospam@nospam. comwrote in message
              news:e5wW7jHyGH A.1936@TK2MSFTN GP06.phx.gbl...
              |I am doing a Generic.Diction ary(of string,myobject )
              |
              | I need to access the item by key, but also by index. Is this the wrong
              thing
              | to use? Should I be using something different.
              |
              | In other words sometimes I just need item(0), the first item added to the
              | list, but other times by key.
              |
              | TIA,
              |
              | Shane
              |
              |


              Comment

              • SStory

                #8
                Re: Generic collection question

                Thanks Jay

                "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.netw rote in
                message news:ueEjYsKyGH A.4232@TK2MSFTN GP05.phx.gbl...
                SStory,
                If I need a generic collection with access by key & index I will normally
                use a System.Collecti ons.ObjectModel .KeyedCollectio n(Of TKey, TItem).
                Especially when used as a base class.
                >
                --
                Hope this helps
                Jay B. Harlow [MVP - Outlook]
                .NET Application Architect, Enthusiast, & Evangelist
                T.S. Bradley - http://www.tsbradley.net
                >
                >
                "SStory" <nospam@nospam. comwrote in message
                news:e5wW7jHyGH A.1936@TK2MSFTN GP06.phx.gbl...
                |I am doing a Generic.Diction ary(of string,myobject )
                |
                | I need to access the item by key, but also by index. Is this the wrong
                thing
                | to use? Should I be using something different.
                |
                | In other words sometimes I just need item(0), the first item added to
                the
                | list, but other times by key.
                |
                | TIA,
                |
                | Shane
                |
                |
                >
                >

                Comment

                Working...