VB Collections - Accessing the collection, not the item. 
Author Message
 VB Collections - Accessing the collection, not the item.

I have programmed a string of collections using objects so that the client
code can look like this:

Parents("Father").Children("Son").Grandchildren("Baby").

If I want to add a Parent, I can write this in my code:
Parents.Add (string).

If I want to add a Child, I can write this in my Code:
Children.Add (string)

However, if I want to add a Child which belongs to a specific parent, I have
problems.  I would like to write this in the client code:
Parent("Father").Children.Add    (This generates a compiler error "Argument
[for children] not optional)

Once I type in   "Parent("Father).Children"  I do NOT have access to the
Children collection, rather VB demands that I give a key and that I access a
specific Child within the Children collection. So I must then type
"Parent("Father").Children("ChildKey").

 It no longer lets me access the Children collection.  Why?

In VBA for Excel there does not seem to be a problem when I try to access
built-in objects and collections.
For example If I type:
"Workbooks(1).Worksheets."  This gives access to the worksheets collection
(Add method available)
"Workbooks(1).Worksheets(1)" this gives access to a specific worksheet (Add
method no longer available).

In other words, in VBA the program knows if I do not specify a key for
Worksheets, I am exposed to the collection. Access to an individual
Worksheet is optional, by providing the key.

Why is VB different, or am I doing something wrong?



Wed, 04 Aug 2004 07:29:09 GMT  
 VB Collections - Accessing the collection, not the item.
Hello Tran
You didn't include the definition of the objects you are returning and you
use the term "String of collections".  What do you mean by this term?  In
order to use this type of syntax the object returned from the collection
must support the ".Children" method/property and the children object
returned must be a collection for the ".Add" method to be valid.  From what
I see you should have two classes defined like the following:

Class  "Parent"
  Public Children as collection

Class "Child"
  Public Grandchildren as collection

Then you can create the Parents collection in a module and do code like the
following:

    Dim Temp as Child
    Set Temp = new Child      ' this creates a new child object and a
collection within called Children
    Parents("Father").Children.Add Temp, "Son"    ' Add the new child to the
parent object collection of children

Hope this helps.  What I see you doing here looks like a good use of
"Shapes" within ADO as this looks like a hierarchial DB.

John


Quote:
> I have programmed a string of collections using objects so that the client
> code can look like this:

> Parents("Father").Children("Son").Grandchildren("Baby").

> If I want to add a Parent, I can write this in my code:
> Parents.Add (string).

> If I want to add a Child, I can write this in my Code:
> Children.Add (string)

> However, if I want to add a Child which belongs to a specific parent, I
have
> problems.  I would like to write this in the client code:
> Parent("Father").Children.Add    (This generates a compiler error
"Argument
> [for children] not optional)

> Once I type in   "Parent("Father).Children"  I do NOT have access to the
> Children collection, rather VB demands that I give a key and that I access
a
> specific Child within the Children collection. So I must then type
> "Parent("Father").Children("ChildKey").

>  It no longer lets me access the Children collection.  Why?

> In VBA for Excel there does not seem to be a problem when I try to access
> built-in objects and collections.
> For example If I type:
> "Workbooks(1).Worksheets."  This gives access to the worksheets collection
> (Add method available)
> "Workbooks(1).Worksheets(1)" this gives access to a specific worksheet
(Add
> method no longer available).

> In other words, in VBA the program knows if I do not specify a key for
> Worksheets, I am exposed to the collection. Access to an individual
> Worksheet is optional, by providing the key.

> Why is VB different, or am I doing something wrong?



Sat, 07 Aug 2004 02:56:29 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Why VBA.Collection and Not VB.Collection?

2. Collections, swapping items in a collection

3. Collection.Add and Collection.Item question?

4. Collections, Collections and More Collections

5. item not found in collection error in qrydef

6. Item not found in Collection Error?

7. Item not in this collection

8. Variable field Names/Item not in Collection

9. Help needed with an item not in a collection..........ACC97

10. Item not found in collection error

11. Item not found in this collection

12. Item Not Found in Collection (Error 3036)

 

 
Powered by phpBB® Forum Software