1 / 6

Collections

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

desma
Download Presentation

Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Collections A collection is a way of grouping a set of related items. Properties and Methods a Collection

  2. 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.

  3. 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

  4. 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

  5. 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).

  6. 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.

More Related