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

Implementing Built-In Collection Features in the Dependent Collection Class

Collection objects have three built-in methods and one built-in property:
  • The Count property, which returns the number of items in the collection

  • The Add method, which adds a new item to the collection and returns a pointer to the new item

  • The Remove method, which removes an item, given its index or unique key

  • The Item method, which returns a pointer to an existing item, given its index or unique key

As mentioned in the preceding section, you don't call these features directly from outside the dependent collection class. Instead, you need to write Public wrapper functions that, of course, become methods of the dependent collection class. These methods then mediate between controller calls to the collection and the collection itself.

The following sections list the wrapper methods you must write for each of these features.

  1. The Collection's Count Property and Class Wrapper Method

  2. The Collection's Add Method and Class Wrapper Method

  3. The Collection's Delete Method and Class Wrapper Method

  4. The Collection's Item Method and Class Wrapper Method

  5. Initializing the Collection in the Parent Class

The Collection's Count Property and Class Wrapper Method

You need to write a Count method in the dependent collection class to wrap the collection's Count property. This method just calls the Private Collection object's Count property and returns its value, as in Listing 12.7.

LISTING 12.7
A WRAPPER METHOD FOR THE COLLECTION'S COUNT PROPERTY

   Public Function Count() As Long
      Count = colFiles.Count
   End Function

The Collection's Add Method and Class Wrapper Method

You need to write an Add method in the dependent collection class to wrap the collection's Add method. You will implement this as a Function returning an object variable pointing to the newly added item, as in Listing 12.8. Notice that you pass the wrapper Add method any properties that you want to assign to the new object you will create. You initialize a new object variable whose type is an object of the dependent class. You then assign the properties that were passed into the wrapper and call the built-in Add method of the Collection object to add the newly created object to the Collection and give it a key value. Finally, you set the return value of your wrapper method to point to the newly added item.

LISTING 12.8
A Class ADD METHOD TO WRAP THE COLLECTION'S ADD METHOD

Public Function Add (ByVal Name As String, _
   ByVal Path As String) _
   As File

  'Initialize new object variable
  Dim filNew As New File

  'assign parameters to its
  'properties
  FilNew.Name = Name
  FilNew.Path = Path

  'Add it to the collection
  colFiles.Add filNew, Name
   
  'Let the return value of this
  'function point to it
  Set Add = filNew
End Function

The Collection's Delete Method and Class Wrapper Method

You need to write a Remove method in the dependent collection class to wrap the collection's Remove method. You will implement this as a Sub procedure that takes an argument indicating the index in the collection of the object to be deleted, as in Listing 12.9. Notice that the parameter is declared as a Variant rather than as an Integer or a Long as you might expect for an Index value. The reason for this is that controlling code can use either a numeric index or a String-type key value to look up an item in the collection.

LISTING 12.9
A WRAPPER FUNCTION FOR THE COLLECTION'S REMOVE METHOD

Public Sub Remove (ByVal Index As Variant)
   colFiles.Remove Index
End Sub

The Collection's Item Method and Class Wrapper Method

The Item method you will write wraps around the collection's builtin Item method, as you can see in Listing 12.10. The method will return a pointer to a particular item in the collection. To do so, it must take an Index parameter, which can be either an Integer array index of a unique key of type String. As with the Remove method, this parameter must be declared as a Variant to accommodate both these possibilities.

LISTING 12.10
AN ITEM WRAPPER METHOD FOR THE COLLECTION'S BUILT- IN ITEM METHOD

Public Function Item _
(ByVal Index As Variant) _
As Variant
   Set Item = colFiles.Item(Index)
End Function

Initializing the Collection in the Parent Class

The Parent class shows us just the tip of the collection's iceberg. It doesn't have to do much to implement this custom object class collection, but it does need to refer to the collection, because it provides the gateway to the collection by being the only externally creatable class.

You need to put a statement in the Parent class's General Declarations that initializes a pointer to the dependent collection class. If the dependent collection class were named Files, for example, you would put a line in the Parent class's General Declarations that would read as follows:

Public Files As New Files


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved