60 likes | 270 Views
Collections. A collection is a way of grouping a set of related items. Properties and Methods a Collection. How is a Collection stored?. A Collection object stores each item in a Variant. The list of things you can add to a Collection object is the
E N D
Collections A collection is a way of grouping a set of related items. Properties and Methods a Collection
How is a Collection stored? • A Collection object stores each item in a Variant. • The list of things you can add to a Collection object is the • same as the list of things that can be stored in a Variant. • This include standard data types, objects, and arrays. • Variants always take up 16 bytes, no matter what's stored in them. • Using a Collection object is not as efficient as using arrays. • However, you never have to ReDim a Collection object • Collection objects have extremely fast look-ups by key, which arrays do not. • Note: A Variant always takes up 16 bytes even if the data are actually stored elsewhere. • TradeOff: Collection objects allow you to write very clean, maintainable code • — at the cost of storing items in Variants.
How is a Collection stored? Use the Item method to retrieve specific items from a collection. The syntax is: [Set] variable = object.Item(index) Set woCurrent = colWorkOrders.Item(3) or Set woCurrent = colWorkOrders.Item("W017493") Item is the default method Set woCurrent = colWorkOrders(3) or Set woCurrent = colWorkOrders ("W017493") colWorkOrders("W017493").Priority = 3
colCourses objCourse objCourse objCourse objCourse objCourse Creating your own collection class The most robust way to implement a collection is by making it a class module . Create a class that is a wrapper around a collection. colCourses collection The student/courses project demonstrates this. clsStudent colCourses clsCourse colCourses is a container for objects of type clsCourse
Making Item the Default Property • To make Item the default property • On the Tools menu, click Procedure Attributes to open the Procedure Attributes dialog box. In Name box, select the Item method. • Click Advanced to show the advanced features. In the Procedure ID box, select (Default) to make the Item method the default. Click OK. • Note A class can have only one default member (property or method).
Enabling For Each • Write the NewEnum property • Public Property Get NewEnum() As Iunknown • Set NewEnum = mcol.[_NewEnum] • End Property • To hide the NewEnum method and give it the necessary procedure ID • On the Tools menu, click Procedure Attributes to open the Procedure Attributes dialog box. In Name box, select the NewEnum method. • Click Advanced to show the advanced features. Check Hide this member to make NewEnum hidden in the type library. • In the Procedure ID box, type –4 (minus four) to give NewEnum the procedure ID required by For Each … Next. Click OK. • Important In order for your collection classes to work with For Each … Next, you must provide a hidden NewEnum method with the correct procedure ID.