Extending the Collection class
You can now use it by declaring an instance of the Files class and repeatedly call the class's Add method for each file that was once found by the Dir function. But this is not the work we want to accomplish in our example. Why not? One of the advantages of using the Collection class is that you can extend its functionality by adding more properties and methods; it is not limited to Add, Remove, Item, and Count.
In the case of our Files class, isn't it going to do more testing of the code that places the scan directory in the class itself, not in every application that uses it? This is a guiding principle of object-oriented design: Place the code closest to where it is needed.
To illustrate this concept, we add a Path property to the Collection class. When you set this property, this class will scan the directory and add every file it finds to the Collection object (see Figure 4).
When a procedure changes the Path property of this class, it triggers the Property Let procedure. In our case, it happened after you selected a path from the browser dialog. Here's a simple code to fill the Collection task with a list of files.
` Reinitialize the collection.
Set mobjFiles = New Files
` Set the path property.
mobjFiles.Path = strPath
Once the Path property is set, the Files class will popularize its own Collection and make it available to the application. This is another example. In this example, the "guts" of the procedure exist inside the class itself (you can prove that the class no longer requires its Add and Remove methods. In some applications, this may be the case. , but for the purposes of illustration we have chosen to leave them as part of this class).
Some disadvantages
The life of using the VBA Collection class is not always wine and roses. When you use the Collection class instead of the Collection object, you must give up two things. The first is the default method of the Collection object, Item. The default method allows you to omit the word "Item" from your code. For example, the following two statements are the same, assuming colFiles references a Collection object.
Debug.Print colFiles.Item(1).Size
Debug.Print colFiles(1).Size
Unless you are using Visual Basic 5.0, there is no way to specify a default method for a class. Therefore, you must always explicitly call the Item method.
The second major disadvantage of the Collection class is the inability to create enumeration functions. Enumeration classes are classes that enable the For Each loop to work. If you want to repeat every item in the Collection, you must do it in the old fashion, using the Count property and the For Next loop. For example, the following code can popularize the list box:
` Fill the list box with info.
lstFiles.Clear
For lngCount = 1 To mobjFiles.Count
With mobjFiles.Item(lngCount)
lstFiles.AddItem .ShortName & _
Space(12 - Len(.ShortName)) & _
vbTab & .AttributeString & _
vbTab & .Size
End With
Next
Notice how this process uses the counter variable lngCount to complete the loop from 1 to the number of items in the Collection. The With statement uses the Item method to refer to each object in the Collection.
Please note that Visual Basic 5.0 users can overcome this limitation by creating enumeration functions. Search the keyword "enumeration" in Visual Basic Book Online for more detailed information.
The test of the future
The use of the Collection class is very extensive. Not only does it produce beautiful collections (important when they are used by others), but by adding other properties and methods, you can create extremely powerful classes. In later publications, we will show you more examples of using the Collection class.
Ken Getz and Mike Gilbert are senior advisors to MCW Technologies. Their research focuses on the Visual Basi and Offic and BackOffice groups. They recently completed two books, VBA Developer`s Handbook and Access 97 Developer`s Handbook (compiled with Paul Litwin), both published by SYBEX.
Safety Razor For Men,Razor For Private Area Male,Shaving Trimmer Machine,Electric Shaver Argos
Ningbo Chuangxiang Electrical Appliance Co.,Ltd , https://www.haircurler-shaver.com