Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 12

Storing Multiple Instances of an Object in a Collection

A controller may need to create an indefinite number of copies of a single object class. The FileFind method might instantiate a special File object for every file it finds, for instance. You don't know, however, exactly how many Files might get created. You have already seen a concept in VB that enables you to implement something like this: the collection.

To implement your own custom collection in a project, you need to add two classes:

These classes are in addition to at least one other class that probably already exists in your project: the parent class, which is considered to "own" the dependent collection class.

By convention, a to dependent class Name is a singular noun and a dependent collection class Name is the plural form of the same noun. You might name a dependent collection class Files and a dependent class File, for example.

Setting Up the Dependent Class

To start a dependent class in your server project, just insert a class module into the project and make sure that the new module is named appropriately. As mentioned earlier, the name of the dependent class should be a singular noun.

You can then give the dependent class any features you need individual elements of the collection to have, such as custom properties, methods, or events.

Setting Up the Dependent Collection Class

Once again, insert a class module into the project and make sure that it is named appropriately: Typically, the name will be the plural form of the dependent class name.

Now, in the General Declarations section of the newly inserted module, declare a Private Collection variable, for example:

   Private colFiles As New Collection

The Collection type has special methods and properties discussed in the next section that enable you to implement a collection of objects.

You declare the Collection in the General Declarations section so that the collection gets initialized as soon as an object is instantiated from this class. You declare it as Private so that controller code can't directly manipulate the Collection object. Instead, the controller code will have to go through wrapper methods (Public procedures) that you create in this class.

Implementing Built-In Collection Features in the Dependent Collection Class


  

<< Previous | Contents | Next >>

 

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved