Some ActiveX components provide array or Collection properties. For example the . List Box control exposes the List property which is an array. The items of the List Box control are stored in the List array which is a property of the control. and they can l accessed by an index that goes from 0 to list Count( -1).Implementing array properties from within your component isn’t complicated but you must declare an array and the methods for adding retrieving and removing elements from the array. Arrays aren’t the best structures for storing multiple items because you can’t quickly remove an element of the array. To delete an element you must move all the elements that follow up by one position. A structure that’s better suited for this type of task is Use Collection object.
Implementing a Collection
To implement a public collection in your custom component you must expose the following properties and methods of the Collection object:
• Add Method Adds a new item to the collection
• Remove Method Removes an item from the list
• Clear Method Removes all items from the list
• Count Property Returns the number of items in the list
• Item Property Returns a specific item
To do so insert the following collection declaration at the beginning of the Class Module’s Code window:
Dim Data Collection As New collection
Data Collection is a new variable that can hold multiple items. Then insert the following property and method definitions so the developer can manipulate the collection (add/remove items to and from the collection enumerate it and retrieve selected items by their key). The Class’ Count property returns the value of the collection’s Count property.
The Count Property Of Data Collection
Public Property Get Count() As Long
Count Data Collection.Count
The Add method is a public function.that returns True if its arguments are successfully added to the collection. If an error occurs then the method returns False.
The Add Method.of DataColiection
Public Function Add(d Value As Double) As Boolean
On Error Go To Add Error
Data Collection.Add d Value !
Add = True
Add = False
The Remove method is also a Boolean function that returns True if the So ” element is successfully removed from the list. In addition, it generates a run time error, which the host application can with the On statement Notice that the Err.Raise statement in Code 15.9 triggers a trappable error with a number and a description (you must add the vb Object Error constant ~ avoid conflicts with the standard Visual Basic error message). As far as the application that uses the Class is concerned it’s as if it has received a visual Basic error. Notice that Visual Basic may trigger its own error 1.our error this for our custom Class which is why we added the vb Object Error constant.
The Remove Method of Data Collection
Public Function Remove(index As Long) As Boolean
If index < 0 Or index> data Collection.Count Then
Data Collection.Remove index .
Remove = True
Err.Raise vb Object Error + 2 ‘Class Name’
couldn’t remove item’
Remove = False
The syntax of the Raise method of the Err’object is:
Err.Raise number Source’ description
source is the name of the component where the error occurred (it’s possible that multiple components will raise the same error). The host application must read both the error’s number and source and-then take some action (e.g. correct the condition that triggered the error, repeat the operation).
The Item method a specific item from the collection based ‘on the index . that’s passed to the method as argument. U the index is beyond the collection’s bounds the trappable run time error 2 is triggered .
The Item Method of Data Collection
Public Function Item(ind.x As Long) As Double
If index < 0 Or index> Data Collection.Count Then
Err.Raise vb Object Error + 1 Class Name· _
“Index out of bounds
Item – Data Collection(index)
The last method is the Clear method, which removes.all the items in the collection. Because the collection doesn’t have a Clear method, we must remove every item in the collection with its Remove method. Notice also that the loop scans the collection’s items backward. A forward loop would cause a run time error because after the removal of the first item, the Data Collection. Count property will be less by one; however; this change won’t affect the number of iterations.
The Clear Method of Data Collection
Public Function Clear() As Boolean
On Error Go To Clear Error
For 1 Data Collection Count To 1 Step -1
Data Collection remove i
Clear = True
Clear = False
VB6 at Work: The AX5tat Class
The AXStat Class project demonstrates the process of designing Classes that expose collections. The AXStat Class exposes the members of a standard collection and the following custom in members:
Average property Returns the average of the data set currently stored in the control
Min property Returns the minimum value in the data set Max property Returns the maximum value in the data set The custom properties are quite trivial and their implementation is shown next.