Results 1 to 13 of 13

Thread: collection class

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    collection class

    Hey,

    I have a collection class that inherits from hashtable.
    I use a sql statement to get the items and then populate the internal hashtable with teh results. However, the items are sorted automatically by the hashtable and I sort them in teh SQL statement.
    Is there any way to have a collection class that inherits from something that doesn't sort the items in the collection? I want them to be in teh same order as when I added the items.


    Thanks,
    Don't anthropomorphize computers -- they hate it

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: collection class

    How about inheriting from CollectionBase instead? it doesn't use keys (just number indexes)

    do you need to use keys?

  3. #3

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Re: collection class

    yeah, I want to be able to access an item by its key which is the internal id in teh database table.
    I don't want to have to go through the whole collection to find what I want.
    Don't anthropomorphize computers -- they hate it

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: collection class

    ok how about dictionarybase? its key based, I am not sure about the sorting, but it should be an easy test for you.

  5. #5

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Re: collection class

    I tried, it puts them in some weird order though. I want to retain the order in which I put them into the collection.
    Don't anthropomorphize computers -- they hate it

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: collection class

    what are you sorting by in the SQL statement? or does it vary from time to time?

  7. #7
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: collection class

    what do you mean putting them in a wierd order?
    if I put elements into a dictionary object then access then with for each they are accessed in the same order I put them in.

    Perhaps you can show a code snippet?

  8. #8

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Re: collection class

    yeah, because you probably put them in order. the items I put in might have ids that are not in order if I sort them based on dates for example.
    Most of the collection classes sort the item based on the key. my keys can be something like 1, 14, 7, 22 etc. They are the actual ids in a database table.

    Thanks,
    Don't anthropomorphize computers -- they hate it

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: collection class

    I didn't think that Hashtables altered the order either but I just tested it and it did indeed order the items by key. I guess there must be no guarantee that this will be the case though, otherwsie the SortedList wouldn't exist. To solve your issue you could keep an internal ArrayList or some other collection with the keys in the order you want. When you want to get the items you would then iterate through the ArrayList to get the keys in order, and use each key to get the corresponding value from the Hashtable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: collection class

    OK I'm must be dense,

    If I do this
    VB Code:
    1. Dim myList As New Dictionary(Of Integer, String)
    2.         myList.Add(1, "Item 1")
    3.         myList.Add(14, "Item 2")
    4.         myList.Add(7, "Item 3")
    5.         myList.Add(22, "Item 4")
    6.  
    7.  
    8.         For Each kvp As KeyValuePair(Of Integer, String) In myList
    9.             Debug.Print("{0}  {1}", kvp.Key, kvp.Value)
    10.         Next
    I get this
    1 Item 1
    14 Item 2
    7 Item 3
    22 Item 4
    What are you doing differently?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: collection class

    Generic collections may behave differently, but the original post said Hashtable. I just tried this code:
    VB Code:
    1. Dim myList As New Hashtable
    2.         myList.Add(1, "Item 1")
    3.         myList.Add(14, "Item 2")
    4.         myList.Add(7, "Item 3")
    5.         myList.Add(22, "Item 4")
    6.  
    7.         For Each n As Integer In myList.Keys
    8.             Debug.Print("{0}  {1}", n, myList(n))
    9.         Next
    10.  
    11.         For Each de As DictionaryEntry In myList
    12.             Debug.Print("{0}  {1}", de.Key, de.Value)
    13.         Next
    14.  
    15.         Dim ie As IEnumerator = myList.GetEnumerator()
    16.         Dim d As DictionaryEntry
    17.  
    18.         While ie.MoveNext()
    19.             d = DirectCast(ie.Current, DictionaryEntry)
    20.             Debug.Print("{0}  {1}", d.Key, d.Value)
    21.         End While
    and I got this output:

    7 Item 3
    14 Item 2
    1 Item 1
    22 Item 4
    7 Item 3
    14 Item 2
    1 Item 1
    22 Item 4
    7 Item 3
    14 Item 2
    1 Item 1
    22 Item 4

    As you can see, not the order that they were added or properly sorted. I tried it previously using the second and third methods above and with different data and the items were in key order. As I said previously, the Hashtable makes no representations about what order its items will be returned in. I think it actually depends on the keys themselves as they may be ordered by hash bucket.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: collection class

    So, a generic dictionary object would work?

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: collection class

    Here's a quote from the help topic for the Generic.Dictionary class:
    For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair structure representing a value and its key. The order in which the items are returned is undefined.
    This is the same situation as for the Hastable, which the help also says that the Dictionary is implemented as. It may be that in some tests the items are returned in a particular order but there is no guarantee that that will be the case for all situations.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width