How to copy
Of course, once you have declared the Collection object as Private, there is no way to add or remove items from it for any process. Therefore, the next step is to copy the standard methods of the Collection object. Although it sounds like we have done a lot of extra work, we are still actually doing the "filtering" work we mentioned earlier.
Remember that the built-in Collection object has an Add method that accepts object references and unique identifiers that include literals and numbers. If your application is using the Collection object directly, it will most likely create a new instance of the object and add it to the Collection itself.
` Create a new instance of an object.
Dim objFile As New File
objFile.Path = "C:\AUTOEXEC.BAT"
` Add to a Collection object.
colFiles.Add objFile, objFile.ShortName
Applying the Collection class, the application calls the Add method of the class, passing any necessary information. Please compare the previous code with the Add method of the Files class:
With a Collection class, the application calls the Add method of the class, passing any required information. Contrast the previous code with the Add method of the Files class:
Public Function Add(Path As String) As File
Dim objFile As File
` Create the new File object.
Set objFile = New File
objFile.Path = Path
` Add it to the Private collection.
pcolFiles.Add objFile, objFile.ShortName
` Return a pointer to the new object.
Set Add = objFile
End Function
In this case, object creation and addition to the Collection occur inside the Add method; the class retains complete control. Any necessary information (such as the path to the file) is provided as a parameter to the method. The code that is called by the application to add the file to the Collection can then be simplified to:
` Add a file to the collection.
colFiles.Add "C:\AUTOEXEC.BAT"
In addition to the Add method, the Collection class should also implement the Item and Remove methods, as well as a Count property:
Public Function Item(Key As Variant) As File
` Return an item in the collection.
Set Item = pcolFiles.Item(Key)
End Function
Public Sub Remove(Key As Variant)
` Remove an item from the collection.
pcolFiles.Remove Key
End Sub
Property Get Count() As Long
` Return the number of items.
Count = pcolFiles.Count
End Property
Please note that in these three methods, we have omitted error handling - there are some things you should never do! At a minimum, you should include an error handler that passes the error to the calling process by using the Raise method of the Err object.
` Private variable to store path.
Private pstrPath As String
Property Get Path() As String
` Return stored path value.
Path = pstrPath
End Property
Property Let Path(strPath As String)
Dim strFile As String
`Clear the collection.
Set pcolFiles = New Collection
`Make sure there`sa backslash.
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
` Get the first file.
strFile = Dir(strPath & "*.*", _
vbReadonly Or vbHidden Or vbArchive Or vbSystem)
Do Until Len(strFile) = 0
` Add it to the collection.
Call Add(strPath & strFile)
` Get the next file.
strFile = Dir()
Loop
` Save the path.
pstrPath = strPath
End Property
Figure 4 Adds the Path property to the Collection class. Set the property and class to scan the directory and add each file found to the private Collection object.
Aluminum Ruler,Office Stationery,Ruler Set
School Stationery Co., Ltd. , http://www.cn-stationerys.com