Board index » Visual Studio » Key and collection
|
onetrevorgranthotmailcom
|
|
onetrevorgranthotmailcom
|
Key and collection
Visual Studio16
is there a way to find out if a particular key already exists in a collection without trying to retrieve it and trap the error ? - |
| AGP
Registered User |
Wed Aug 24 23:01:32 CDT 2005
Re:Key and collection
From the work i have done no. but you can actually loop through collection
and then check your key aginst those that you find. cool if your collection is small but inefficient if the collection is big. AGP "PC" <Onzin@pandora.be>wrote in message Quoteis there a way to find out if a particular key already exists in a - |
| PC
Registered User |
Wed Aug 24 22:16:57 CDT 2005
Re:Key and collectionQuoteFrom the work i have done no. but you can actually loop through collection how can i check wheter or not my key is in the collection with other words how can i find out wich keys are in a collection ? - |
| Jeff
Registered User |
Wed Aug 24 23:46:10 CDT 2005
Re:Key and collection"AGP" <sindizzy.pak@softhome.net>wrote in message QuoteFrom the work i have done no. but you can actually loop through collection resort to API calls or trap an error. - |
| Rick
Registered User |
Thu Aug 25 00:00:00 CDT 2005
Re:Key and collectionQuote>From the work i have done no. but you can actually loop through and use InStr to see if the key has been used before or not. Note to OP... a delimiter must be used to start, end and separate the keys in this String variable and the InStr must search using the sought after key surrounded by this delimiter. This is necessary to make sure no false positive hits are found. Something like this, assuming an asterisk for the delimiter (of course, you have to pick a delimiter that will never appear in any of the keys) Keys = "*" CollectionVariable.Add "SomeValue", "KeyNumberOne" Keys = Keys & "*" & "KeyNumberOne" & "*" .... .... CollectionVariable.Add "AnotherValue", "KeyNumberTwo" Keys = Keys & "*" & "KeyNumberTwo" & "*" .... .... HasKeyBeenUsed = CBool(Instr(Keys, "*" & PossibleKey & "*")) You can remove a key using the Replace function.... Keys = Replace(Keys, "*" & KeyToRemove & "*", "") Kind of off the top of the head, but it should work. An alternative to this, of course, would be to store the Keys in an array as they are created and loop the array to test if a possible key is in there or not. Rick - |
| Jan
Registered User |
Thu Aug 25 02:48:13 CDT 2005
Re:Key and collection
"PC" <Onzin@pandora.be>'s wild thoughts were released on
Thu, 25 Aug 2005 04:57:17 +0200 bearing the following fruit: Quoteis there a way to find out if a particular key already exists in a I prefer to add classes to my collections rather than a plain old sting or integer for example. That way I can add a lot more information that just one piece of data. Having said that I usually do want to add a lot more information. It also means you can expose a key. Jan Hyde (VB MVP) -- "You don't see the point, do you?" asked Tom, making a stab in the dark. (Kegel Archives) [Abolish the TV Licence - www.tvlicensing.biz/]">www.tvlicensing.biz/] - |
| Larry
Registered User |
Thu Aug 25 08:12:06 CDT 2005
Re:Key and collection"Jan Hyde" <StellaDrinker@REMOVE.ME.uboot.com>wrote Quote>is there a way to find out if a particular key already exists in a Dim c As New Collection Dim x As Long c.Add Me, "Form1" x = c("Form1") ' error 450 The collection might hold anything, so either you have to know precicely what is in the collection, or you'd end up having a couple of paths to handle after checking the error code. Attempting to add the key is (IMHO) a better solution because it throws only one type of error, that of a duplicate key: On Error Resume Next c.Add 0, key If Err.Number Then ' Key is present Else ' Key is not present c.Remove key End If LFS - |
| MikeD
Registered User |
Thu Aug 25 08:42:24 CDT 2005
Re:Key and collection"Jeff Johnson [MVP: VB]" <i.get@enough.spam>wrote in message Quote
Or write your own collection class which exposes the keys, perhaps returning them in an array. -- Mike Microsoft MVP Visual Basic - |
| PC
Registered User |
Thu Aug 25 10:05:24 CDT 2005
Re:Key and collection"Jeff Johnson [MVP: VB]" <i.get@enough.spam>wrote in message Quote... - |
| PC
Registered User |
Thu Aug 25 10:11:03 CDT 2005
Re:Key and collection"Jan Hyde" <StellaDrinker@REMOVE.ME.uboot.com>wrote in message QuoteI prefer to add classes to my collections rather than a just to be used as the key into the collection and i still need to loop trough the collection to retrieve its objects, just to find out if a key exist - |
| PC
Registered User |
Thu Aug 25 10:20:41 CDT 2005
Re:Key and collection"Larry Serflaten" <serflaten@usinternet.com>wrote in message QuoteAttempting to add the key is (IMHO) a better solution because it ...just come to think of it maybe the key is internally to the collection a hash of the key wich would make it impossible to retrieve its value could that be the case ? - |
| PC
Registered User |
Thu Aug 25 10:14:29 CDT 2005
Re:Key and collection"MikeD" <nobody@nowhere.edu>wrote in message QuoteOr write your own collection class which exposes the keys, perhaps never tried - |
| MikeD
Registered User |
Thu Aug 25 12:43:42 CDT 2005
Re:Key and collection"PC" <Onzin@pandora.be>wrote in message Quote
I never said not to use VB's Collection object. You can still use that, wrapped into your own "collection" class. There's really nothing difficult about it. -- Mike Microsoft MVP Visual Basic - |
| MikeD
Registered User |
Thu Aug 25 12:45:52 CDT 2005
Re:Key and collection"PC" <Onzin@pandora.be>wrote in message Quoteyes thats what i am doing now, but i hate to use error trapping for There's absolutely nothing wrong with trapping errors for something like this. Errors are just information. Use that information to your advantage. -- Mike Microsoft MVP Visual Basic - |
| Jan
Registered User |
Fri Aug 26 02:52:26 CDT 2005
Re:Key and collection
"PC" <Onzin@pandora.be>'s wild thoughts were released on
Thu, 25 Aug 2005 17:11:03 +0200 bearing the following fruit: Quote
have a field which makes for a natural key. If you want to find out if a key exist then you either have to catch the error or loop, there's no magic shortcut I'm afraid. Jan Hyde (VB MVP) -- Zebra: A large undergarment that's an utter farce (Stan Kegel) [Abolish the TV Licence - www.tvlicensing.biz/]">www.tvlicensing.biz/] - |
| PC
Registered User |
Fri Aug 26 04:23:52 CDT 2005
Re:Key and collection
thanks to everyone
think i'll have a good look at the dictionary object, to see if it can replace the collection - |
| MikeD
Registered User |
Fri Aug 26 17:15:51 CDT 2005
Re:Key and collection"PC" <Onzin@pandora.be>wrote in message Quotethanks to everyone Scripting Runtime, which may not be installed (for security reasons) and is also susceptible to other problems. -- Mike Microsoft MVP Visual Basic - |
