For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > August 2005 > Key and collection









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Key and collection
PC

2005-08-25, 3:55 am

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

2005-08-25, 3:55 am

From the work i have done no. but you can actually loop through collection
and then check your key aginst those that you find. if your collection
is small but inefficient if the collection is big.

AGP

"PC" <Onzin@pandora.be> wrote in message
news:%237voejSqFHA.272@TK2MSFTNGP15.phx.gbl...
> 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 ?
>
>
>



PC

2005-08-25, 3:55 am

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



Jeff Johnson [MVP: VB]

2005-08-25, 3:55 am


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


Rick Rothstein [MVP - Visual Basic]

2005-08-25, 3:55 am

> > From the work i have done no. but you can actually loop through
collection
>
> 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


Jan Hyde

2005-08-25, 3:55 am

"PC" <Onzin@pandora.be>'s wild thoughts were released on
Thu, 25 Aug 2005 04:57:17 +0200 bearing the following fruit:

>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 - http://www.tvlicensing.biz/]

Larry Serflaten

2005-08-25, 6:55 pm


"Jan Hyde" <StellaDrinker@REMOVE.ME.uboot.com> wrote
>
> 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
MikeD

2005-08-25, 6:55 pm


"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:OoSKk$SqFHA.3136@TK2MSFTNGP11.phx.gbl...
>
> "AGP" <sindizzy.pak@softhome.net> wrote in message
> news:wobPe.2508$u_6.1503@newssvr17.news.prodigy.com...
>
collection[color=darkred]
>
> 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


PC

2005-08-25, 6:55 pm


"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:OoSKk$SqFHA.3136@TK2MSFTNGP11.phx.gbl...
>...
> 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



PC

2005-08-25, 6:55 pm


"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




PC

2005-08-25, 6:55 pm


"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




PC

2005-08-25, 6:55 pm


"Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:u#UlVXXqFHA.2364@tk2msftngp13.phx.gbl...
> 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 ?


MikeD

2005-08-25, 6:55 pm


"PC" <Onzin@pandora.be> wrote in message
news:u8ev4CZqFHA.3060@TK2MSFTNGP09.phx.gbl...
>
> "MikeD" <nobody@nowhere.edu> wrote in message
> news:uZ15LrXqFHA.3524@TK2MSFTNGP10.phx.gbl...
> returning
> 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


MikeD

2005-08-25, 6:55 pm


"PC" <Onzin@pandora.be> wrote in message
news:O12M5CZqFHA.3060@TK2MSFTNGP09.phx.gbl...
> 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


Jan Hyde

2005-08-26, 3:55 am

"PC" <Onzin@pandora.be>'s wild thoughts were released on
Thu, 25 Aug 2005 17:11:03 +0200 bearing the following fruit:

>
>"Jan Hyde" <StellaDrinker@REMOVE.ME.uboot.com> wrote in message
> news:fqtqg117qfe4vl1gcir4301mkge43fj18p@
4ax.com...
>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 - http://www.tvlicensing.biz/]

PC

2005-08-26, 7:55 am

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



MikeD

2005-08-26, 6:55 pm


"PC" <Onzin@pandora.be> wrote in message
news:uqk0$giqFHA.1032@TK2MSFTNGP12.phx.gbl...
> 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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com