Problem getting collection items by key 
Author Message
 Problem getting collection items by key

Hi All,

I am using the Class Builder Utility in VB 6 to create a collection of a
class and all works fine until I try to get an item by its key name rather
than its index. When I try to use its key, I get the error "Invalid
Procedure Call or Argument". For example, in the code:

Dim temp
Dim temp2
Dim temp3
Dim temp4
set temp = new testCollection
set temp2 = temp.add("abc")
set temp3 = temp.add("def")
set temp4 = temp(1)
debug.print temp4.key            <-- this returns "abc" as expected
set temp4 = temp("abc")         <-- this results in the error above.

In my collection class code, the following comment was added by the class
builder utility:
    'used when referencing an element in the collection
    'vntIndexKey contains either the Index or Key to the collection,
    'this is why it is declared as a Variant
    'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
However, it doesn't seem to work as stated. Instead I have to check to see
if the value is numeric or a string, then if it is a string, I have to loop
through the collection and look for the key. This is starting to take a fair
bit of time and effecting my application's performance.

Does anyone know what I am doing wrong above?

Thanks,
Bryan



Sat, 30 Aug 2003 08:59:09 GMT  
 Problem getting collection items by key

Quote:

> Hi All,

> I am using the Class Builder Utility in VB 6 to create a collection of a
> class and all works fine until I try to get an item by its key name rather
> than its index. When I try to use its key, I get the error "Invalid
> Procedure Call or Argument". For example, in the code:

> Dim temp
> Dim temp2
> Dim temp3
> Dim temp4
> set temp = new testCollection
> set temp2 = temp.add("abc")

You don't say how your collection class works, but if its like a regular
VB one ten this is your problem. "abc" is not the key, but what you are
storing. If you want the key to be also "abc" it is:
set temp2 = temp.add("abc","abc")

Quote:
> set temp3 = temp.add("def")
> set temp4 = temp(1)
> debug.print temp4.key            <-- this returns "abc" as expected
> set temp4 = temp("abc")         <-- this results in the error above.

> In my collection class code, the following comment was added by the class
> builder utility:
>     'used when referencing an element in the collection
>     'vntIndexKey contains either the Index or Key to the collection,
>     'this is why it is declared as a Variant
>     'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
> However, it doesn't seem to work as stated. Instead I have to check to see
> if the value is numeric or a string, then if it is a string, I have to loop
> through the collection and look for the key. This is starting to take a fair
> bit of time and effecting my application's performance.

> Does anyone know what I am doing wrong above?

> Thanks,
> Bryan

--
Roger Abbott
http://www.rhaminisys.com
DDE ActiveX Controls for VB. DDE FAQ
Other freeware and shareware


Sat, 30 Aug 2003 21:07:46 GMT  
 Problem getting collection items by key
That got it! Thanks a ton. Something so simple, yet so aggravating...

Cheers,
Bryan


Quote:

> > Hi All,

> > I am using the Class Builder Utility in VB 6 to create a collection of a
> > class and all works fine until I try to get an item by its key name
rather
> > than its index. When I try to use its key, I get the error "Invalid
> > Procedure Call or Argument". For example, in the code:

> > Dim temp
> > Dim temp2
> > Dim temp3
> > Dim temp4
> > set temp = new testCollection
> > set temp2 = temp.add("abc")

> You don't say how your collection class works, but if its like a regular
> VB one ten this is your problem. "abc" is not the key, but what you are
> storing. If you want the key to be also "abc" it is:
> set temp2 = temp.add("abc","abc")

> > set temp3 = temp.add("def")
> > set temp4 = temp(1)
> > debug.print temp4.key            <-- this returns "abc" as expected
> > set temp4 = temp("abc")         <-- this results in the error above.

> > In my collection class code, the following comment was added by the
class
> > builder utility:
> >     'used when referencing an element in the collection
> >     'vntIndexKey contains either the Index or Key to the collection,
> >     'this is why it is declared as a Variant
> >     'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
> > However, it doesn't seem to work as stated. Instead I have to check to
see
> > if the value is numeric or a string, then if it is a string, I have to
loop
> > through the collection and look for the key. This is starting to take a
fair
> > bit of time and effecting my application's performance.

> > Does anyone know what I am doing wrong above?

> > Thanks,
> > Bryan

> --
> Roger Abbott
> http://www.rhaminisys.com
> DDE ActiveX Controls for VB. DDE FAQ
> Other freeware and shareware



Sun, 31 Aug 2003 08:42:47 GMT  
 Problem getting collection items by key

Quote:

>Hi All,

>I am using the Class Builder Utility in VB 6 to create a collection of a
>class and all works fine until I try to get an item by its key name rather
>than its index. When I try to use its key, I get the error "Invalid
>Procedure Call or Argument". For example, in the code:

>Dim temp
>Dim temp2
>Dim temp3
>Dim temp4
>set temp = new testCollection
>set temp2 = temp.add("abc")
>set temp3 = temp.add("def")
>set temp4 = temp(1)
>debug.print temp4.key            <-- this returns "abc" as expected
>set temp4 = temp("abc")         <-- this results in the error above.

Hi Bryan,
instead of answering your question, i am going to propose you one:

why not code your collection(s) this way:

dim abc as New Collection

Dim x as classBlah
Set x = New classBlah

abc.Add x
Set x = Nothing

this builds a collection of classes..... and the classes don't all have to be the same, you can add whatever ones you
want...



Sun, 31 Aug 2003 20:06:44 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. retrieving an item from a collection with key?

2. Retrieving the Collection Item's KEY ??!?

3. Retrieving Collection Item By Key

4. Accessing the Key value of a collection item

5. Accessing the key value of collection items

6. Key Property of Item in a Collection?

7. retrieve item by collection key does not work

8. get key of a collection item

9. Using collection.Item("key")

10. How to get named key of Collection item?

11. Collection.Item - using key and get runtime error?

12. How to get keys of items in the collection

 

 
Powered by phpBB® Forum Software