|
-
Apr 26th, 2006, 02:27 PM
#1
Thread Starter
Frenzied Member
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
-
Apr 26th, 2006, 03:41 PM
#2
Re: collection class
How about inheriting from CollectionBase instead? it doesn't use keys (just number indexes)
do you need to use keys?
-
Apr 26th, 2006, 03:43 PM
#3
Thread Starter
Frenzied Member
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
-
Apr 26th, 2006, 03:46 PM
#4
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.
-
Apr 26th, 2006, 03:47 PM
#5
Thread Starter
Frenzied Member
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
-
Apr 26th, 2006, 03:58 PM
#6
Re: collection class
what are you sorting by in the SQL statement? or does it vary from time to time?
-
Apr 26th, 2006, 05:12 PM
#7
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?
-
Apr 26th, 2006, 10:41 PM
#8
Thread Starter
Frenzied Member
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
-
Apr 26th, 2006, 11:20 PM
#9
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.
-
Apr 27th, 2006, 07:55 AM
#10
Re: collection class
OK I'm must be dense,
If I do this
VB Code:
Dim myList As New Dictionary(Of Integer, String)
myList.Add(1, "Item 1")
myList.Add(14, "Item 2")
myList.Add(7, "Item 3")
myList.Add(22, "Item 4")
For Each kvp As KeyValuePair(Of Integer, String) In myList
Debug.Print("{0} {1}", kvp.Key, kvp.Value)
Next
I get this1 Item 1
14 Item 2
7 Item 3
22 Item 4 What are you doing differently?
-
Apr 27th, 2006, 08:12 AM
#11
Re: collection class
Generic collections may behave differently, but the original post said Hashtable. I just tried this code:
VB Code:
Dim myList As New Hashtable
myList.Add(1, "Item 1")
myList.Add(14, "Item 2")
myList.Add(7, "Item 3")
myList.Add(22, "Item 4")
For Each n As Integer In myList.Keys
Debug.Print("{0} {1}", n, myList(n))
Next
For Each de As DictionaryEntry In myList
Debug.Print("{0} {1}", de.Key, de.Value)
Next
Dim ie As IEnumerator = myList.GetEnumerator()
Dim d As DictionaryEntry
While ie.MoveNext()
d = DirectCast(ie.Current, DictionaryEntry)
Debug.Print("{0} {1}", d.Key, d.Value)
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.
-
Apr 27th, 2006, 09:00 AM
#12
Re: collection class
So, a generic dictionary object would work?
-
Apr 27th, 2006, 10:12 AM
#13
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|