Home All Groups Group Topic Archive Search About
Author
8 Jul 2005 5:20 PM
Stephanie
My memory is not working well, nor apparently is my ability to find
something simple in the help. I have a bit of code that I inherited. I need
to replace the reliance on handling an error and check for the existence of
the item in the collection directly.

I would like to iterate through the collection and determine the value of
the *key*. I see how to get the item, but with Add, Item and Remove being
the only methods... How can I iterate through the items in a collection and
retrieve the key?

Thanks.


    On Error Resume Next

    ' First, see if anything is there. We can't just access the member of
the
    ' collection and assign it to a variable, because we don't know what
type of
    ' variable is in the collection, so we just do a VarType() on it.
    eVarType = VarType(oCollection.Item(sKey))
    If Err.Number = 0 Then
        ' It's in there.
        bRetVal = True
    Else
        ' It's not in there.
        bRetVal = False
    End If

Author
8 Jul 2005 5:40 PM
Jeff Johnson [MVP: VB]
"Stephanie" <IwishICo***@NoWay.com> wrote in message
news:%23hZ2aF%23gFHA.2484@TK2MSFTNGP15.phx.gbl...

> I would like to iterate through the collection and determine the value of
> the *key*. I see how to get the item, but with Add, Item and Remove being
> the only methods... How can I iterate through the items in a collection
> and retrieve the key?

You can't. The VB Collection object doesn't expose this. Someone may come
along and show you a hack to read the underlying memory structure, but
that's about the only way.
Author
8 Jul 2005 5:41 PM
Ken Halter
"Stephanie" <IwishICo***@NoWay.com> wrote in message
news:%23hZ2aF%23gFHA.2484@TK2MSFTNGP15.phx.gbl...
> My memory is not working well, nor apparently is my ability to find
> something simple in the help. I have a bit of code that I inherited. I
> need to replace the reliance on handling an error and check for the
> existence of the item in the collection directly.
>
> I would like to iterate through the collection and determine the value of
> the *key*. I see how to get the item, but with Add, Item and Remove being
> the only methods... How can I iterate through the items in a collection
> and retrieve the key?

There's nothing built in that would allow this.... which is probably why the
code you inherited simply trapped the error.

You can use the "hack" method to get an array that contains the keys... it
works. Not supported, at all... but it works.
http://groups-beta.google.com/group/microsoft.public.vb.general.discussion/browse_thread/thread/be118f3b2d3f1316/00c57400f1a04ce5#00c57400f1a04ce5


--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
8 Jul 2005 11:13 PM
Nicholas D. Krempel
Sweet hack! Haven't seen that one before.

Someone ought to collect a page of VB hacks and/or references to books such
as McKinney's and Curland's.

A VB approach would be to maintain a separate collection of Keys, or write a
wrapper class which does this for you.

Nick Krempel

Show quoteHide quote
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message
news:eQwaRR%23gFHA.1252@TK2MSFTNGP09.phx.gbl...
> "Stephanie" <IwishICo***@NoWay.com> wrote in message
> news:%23hZ2aF%23gFHA.2484@TK2MSFTNGP15.phx.gbl...
>> My memory is not working well, nor apparently is my ability to find
>> something simple in the help. I have a bit of code that I inherited. I
>> need to replace the reliance on handling an error and check for the
>> existence of the item in the collection directly.
>>
>> I would like to iterate through the collection and determine the value of
>> the *key*. I see how to get the item, but with Add, Item and Remove being
>> the only methods... How can I iterate through the items in a collection
>> and retrieve the key?
>
> There's nothing built in that would allow this.... which is probably why
> the code you inherited simply trapped the error.
>
> You can use the "hack" method to get an array that contains the keys... it
> works. Not supported, at all... but it works.
> http://groups-beta.google.com/group/microsoft.public.vb.general.discussion/browse_thread/thread/be118f3b2d3f1316/00c57400f1a04ce5#00c57400f1a04ce5
>
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
Author
10 Jul 2005 3:39 AM
Jeff Johnson [MVP: VB]
"Nicholas D. Krempel" <ndkrem***@blueyonder.co.uk> wrote in message
news:KMDze.109140$Vo6.80348@fe3.news.blueyonder.co.uk...

> Sweet hack! Haven't seen that one before.
>
> Someone ought to collect a page of VB hacks and/or references to books
> such as McKinney's and Curland's.
>
> A VB approach would be to maintain a separate collection of Keys, or write
> a wrapper class which does this for you.

I can't imagine why you'd want to use any approach but the second.