Board index » Visual Studio » Key and collection

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 ?


-
 

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

Quote
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 ?











-

Re:Key and collection

Quote
From the work i have done no. but you can actually loop through collection

and then check your key aginst those that you find



but thats the problem

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 ?







-

Re:Key and collection



"AGP" <sindizzy.pak@softhome.net>wrote in message



Quote
From the work i have done no. but you can actually loop through collection

and then check your key aginst those that you find.



Nope. The VB Collection object does not expose its keys. You have to either

resort to API calls or trap an error.





-

Re:Key and collection

Quote
>From the work i have done no. but you can actually loop through

collection

>and then check your key aginst those that you find.



Nope. The VB Collection object does not expose its keys. You have to

either

resort to API calls or trap an error.



You could also store the keys in a String variable as they are created

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





-

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:



Quote
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 ?





What's wrong with that method?



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/]



-

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

>collection

>without trying to retrieve it and trap the error ?

>



What's wrong with that method?



It can throw an error even when a key is present:



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

-

Re:Key and collection



"Jeff Johnson [MVP: VB]" <i.get@enough.spam>wrote in message

Quote


"AGP" <sindizzy.pak@softhome.net>wrote in message

news:wobPe.2508$u_6.1503@newssvr17.news.prodigy.com...



>From the work i have done no. but you can actually loop through

collection

>and then check your key aginst those that you find.



Nope. The VB Collection object does not expose its keys. You have to

either

resort to API calls or trap an error.





Or write your own collection class which exposes the keys, perhaps returning

them in an array.



--

Mike

Microsoft MVP Visual Basic





-

Re:Key and collection



"Jeff Johnson [MVP: VB]" <i.get@enough.spam>wrote in message

Quote
...

Nope. The VB Collection object does not expose its keys. You have to

either

resort to API calls or trap an error.

you mean it actually is possible with API calls

if so could someone please tell me how







-

Re:Key and collection



"Jan Hyde" <StellaDrinker@REMOVE.ME.uboot.com>wrote in message

Quote
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.

yes that is what i am doing now

but it means the class needs a property it does not really need, it's there

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









-

Re:Key and collection



"Larry Serflaten" <serflaten@usinternet.com>wrote in message

Quote
Attempting to add the key is (IMHO) a better solution because it

throws only one type of error, that of a duplicate key:

yes thats what i am doing now, but i hate to use error trapping for

something that should be easy without 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 ?





-

Re:Key and collection



"MikeD" <nobody@nowhere.edu>wrote in message

Quote
Or write your own collection class which exposes the keys, perhaps

returning

them in an array.

yes but building your own collection class without using the VB collection

and overriding its methods seems like a lot of work ,and it is something i

never tried









-

Re:Key and collection



"PC" <Onzin@pandora.be>wrote in message

Quote


"MikeD" <nobody@nowhere.edu>wrote in message

news:uZ15LrXqFHA.3524@TK2MSFTNGP10.phx.gbl...

>Or write your own collection class which exposes the keys, perhaps

returning

>them in an array.

yes but building your own collection class without using the VB collection

and overriding its methods seems like a lot of work ,and it is something i

never tried





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





-

Re:Key and collection



"PC" <Onzin@pandora.be>wrote in message

Quote
yes thats what i am doing now, but i hate to use error trapping for

something that should be easy without it





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





-

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


"Jan Hyde" <StellaDrinker@REMOVE.ME.uboot.com>wrote in message

news:fqtqg117qfe4vl1gcir4301mkge43fj18p@4ax.com...

>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.

yes that is what i am doing now

but it means the class needs a property it does not really need, it's there

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



Most of the classes I add represent tables in a database and

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/]



-

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







-

Re:Key and collection



"PC" <Onzin@pandora.be>wrote in message

Quote
thanks to everyone

think i'll have a good look at the dictionary object, to see if it can

replace the collection



Just keep in mind that the Dictionary object is part of the Microsoft

Scripting Runtime, which may not be installed (for security reasons) and is

also susceptible to other problems.



--

Mike

Microsoft MVP Visual Basic





-