|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
collectionsomething 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 "Stephanie" <IwishICo***@NoWay.com> wrote in message You can't. The VB Collection object doesn't expose this. Someone may come 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? along and show you a hack to read the underlying memory structure, but that's about the only way. "Stephanie" <IwishICo***@NoWay.com> wrote in message There's nothing built in that would allow this.... which is probably why the 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? 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.. 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.. > "Nicholas D. Krempel" <ndkrem***@blueyonder.co.uk> wrote in message I can't imagine why you'd want to use any approach but the second.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. |
|||||||||||||||||||||||