Collection within collection

P

Peter Kinsman

I wish to create a collection of S36File objects with a number of
properties, one of which is is a collection of S36field objects. I have
created Class Modules for S36File and S36Field, and successfully appended a
file to the S36Files collection, but cannot sort out the syntax for adding a
field. I would have expected to use something like:
S36Files(1);S36Fields.Add S36Field
but I am wondering if the Fields collection exists in its own right rather
than as a property of the Files collection.

Yet again I am trying to run before I have leamed to walk,
but I wondered if anyone else had encountered a similar problem.

Many thanks

Peter Kinsman
 
P

Peter Kinsman

Peter

Thank you very much for the suggestion, and I have already printed out the
articles, but they are for VB6. In the meantime, I have found an article at
http://support.microsoft.com/default.aspx?scid=kb;EN-US;210035 for Access
2000 and another identical one for earlier versions, both of which explain
that:

A limitation of the Microsoft Visual Basic for Applications Collection
object is that it cannot contain a user-defined data type.

I wonder if that limitation does not apply to VB6

Thanks again

Peter

"Peter" <peterguy -at- hotmail -dot- com> wrote in message.
check out this excellent series of articles; they've helped me allot:
http://msdn.microsoft.com/library/d...tml/vbconcreatingyourowncollectionclasses.asp

-Peter
 
P

Peter

Can you post any specific error messages?
You should be able to simply add instances of your classes to collections.
Something like this:

Dim myFile as S36File
Dim myField as S36Field
Dim S36Files as Collection

Set myFile = new S36File
Set myField = new S36Field
Set S36Files = new Collection

.... do stuff with myFile .....
S36Files.Add myFile
.... do stuff with myField ...
Set S36Files(1).S36Fields = myField ' using a Property Set in myFile
S36Files(1).S36Fields.Add myField ' S36Fields is either a collection, or has defined Public Sub Add()

Depending on how you constructed it in your S36File object (Property Set, Add Sub, whatever), the syntax for adding a new S36Field object would most likely be in the form of one of the last two lines.

A bit of a caveat: I'm doing all this in Office 2002. Behavior should be similar in 2000, but I'm not positive it will be.

hth,

-Peter
 
P

Peter Kinsman

Thanks for that Peter. I will try it on Monday.

In the article at
http://support.microsoft.com/default.aspx?scid=kb;EN-US;210035 I notice the
sequence

outsideCollection.Add insideCollection

' Clear the collection - prevents duplication of elements.
Set insideCollection = Nothing

I do not understand why it is necessary to set the insideCollection to
Nothing, but I found that if I did not do the same, when I added an item to
the collection, any existing items were overwritten S36File and S36Field
were created by adding a Class Module with Public variables for the various
properties. Although I think I did use Property Set Get and Let in one
project, I do not really understand them, which brings us to the old impasse
that until you understand the topic, the book(s) do not make sense.

Having now read to the caveat, I am still using Office 97 where possible as
a lowest common denominator. As there is not an article for releases later
than 2000, I wonder if the restriction no longer applies, which would
explain why it works for you in 2002.

I will let you know how I get on, but the construction is similar to the one
I used without success.

Regards

Peter

"Peter" <peterguy -at- hotmail -dot- com> wrote in message
Can you post any specific error messages?
You should be able to simply add instances of your classes to collections.
Something like this:

Dim myFile as S36File
Dim myField as S36Field
Dim S36Files as Collection

Set myFile = new S36File
Set myField = new S36Field
Set S36Files = new Collection

.... do stuff with myFile .....
S36Files.Add myFile
.... do stuff with myField ...
Set S36Files(1).S36Fields = myField ' using a Property Set in myFile
S36Files(1).S36Fields.Add myField ' S36Fields is either a collection, or has
defined Public Sub Add()

Depending on how you constructed it in your S36File object (Property Set,
Add Sub, whatever), the syntax for adding a new S36Field object would most
likely be in the form of one of the last two lines.

A bit of a caveat: I'm doing all this in Office 2002. Behavior should be
similar in 2000, but I'm not positive it will be.

hth,

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top