Iterate through a hashtable, a get an object by Position 
Author Message
 Iterate through a hashtable, a get an object by Position

Hashtables allow you to retrieve data by index using ITEM:

    x = ThisFields.Item("RequiredLevel").DBColumnName

This returns the object because I know the key name.
However, these key names are not always known.

I want to be able to iterate through the objects without knowing the names.
Of simply grab the 5th one for example.
Any ideas?

Thanks



Wed, 21 Sep 2005 04:21:51 GMT  
 Iterate through a hashtable, a get an object by Position
Hi James!

Quote:

>Hashtables allow you to retrieve data by index using ITEM:

>    x = ThisFields.Item("RequiredLevel").DBColumnName

>This returns the object because I know the key name.
>However, these key names are not always known.

>I want to be able to iterate through the objects without knowing the names.
>Of simply grab the 5th one for example.
>Any ideas?

In VB, you can iterate through the hashtable using a For Each statement,
such as:

        Dim h As New System.Collections.Hashtable()

        h.Add("one", "one")
        h.Add("two", "two")
        h.Add("three", "three")

        Dim o As System.Collections.DictionaryEntry
        For Each o In h
            Debug.WriteLine(o.Key)
            Debug.WriteLine(o.Value)
        Next

So, you should be able to For Each over the ThisFields collection and if
you want to grab the "5th" element you will need to keep track of the count
through the loop.

I hope this helps!
Chris  Dias
Microsoft Visual Basic Team



Wed, 21 Sep 2005 05:38:27 GMT  
 Iterate through a hashtable, a get an object by Position
I may be wrong but if the hash table collection works the same way as in C#,
there is no warranty that the second element will be "two" or the third
element will be "three" etc. I have a feeling he want the element in the
collection to retain the location in the order in which they were entered
and I don't think this is going to happen with a hash table.



Quote:
> Hi James!

> >Hashtables allow you to retrieve data by index using ITEM:

> >    x = ThisFields.Item("RequiredLevel").DBColumnName

> >This returns the object because I know the key name.
> >However, these key names are not always known.

> >I want to be able to iterate through the objects without knowing the
names.
> >Of simply grab the 5th one for example.
> >Any ideas?

> In VB, you can iterate through the hashtable using a For Each statement,
> such as:

>         Dim h As New System.Collections.Hashtable()

>         h.Add("one", "one")
>         h.Add("two", "two")
>         h.Add("three", "three")

>         Dim o As System.Collections.DictionaryEntry
>         For Each o In h
>             Debug.WriteLine(o.Key)
>             Debug.WriteLine(o.Value)
>         Next

> So, you should be able to For Each over the ThisFields collection and if
> you want to grab the "5th" element you will need to keep track of the
count
> through the loop.

> I hope this helps!
> Chris  Dias
> Microsoft Visual Basic Team



Wed, 21 Sep 2005 07:07:15 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Iterate through an object's properties (any object)

2. Problems with variables retaining values with Hashtable of Objects

3. Storing Objects in a Hashtable

4. Collection Objects vs. HashTable

5. Iterate through object fields recursively

6. iterate object atributes

7. Iterating through the properties of an object?

8. MS Word Table object: iterating through rows?

9. Iterating a dictionary object?

10. iterating over a drives collection in a filesystem object

11. Iterating through properties of a VB com object at runtime

12. Iterating through the properties of an object

 

 
Powered by phpBB® Forum Software