Custom Collections Why use Custom Collections Standard lists

  • Slides: 18
Download presentation
Custom Collections

Custom Collections

Why use Custom Collections? • • Standard lists and dictionary types – Array. List

Why use Custom Collections? • • Standard lists and dictionary types – Array. List – Hashtable Bindable to ASP. NET controls

Problem? • Arrays can hold any type of object… • Can’t be limited to

Problem? • Arrays can hold any type of object… • Can’t be limited to only hold objects of a specific type • So you must check or cast any type being returned

Solution? Custom Collections! • Enforce type safety • Only accept objects of a certain

Solution? Custom Collections! • Enforce type safety • Only accept objects of a certain types

Student business object • Properties: First. Name, Last. Name, Age, Grade • Default and

Student business object • Properties: First. Name, Last. Name, Age, Grade • Default and overloaded constructor – initializes instance properties immediately • Property. Changed event – raised in the Set block

Type-safe custom collection • Only allow objects of a certain type to be added

Type-safe custom collection • Only allow objects of a certain type to be added • Naming convention – “Collection” after the type: Student. Collection

3 ways to implement: • Derive from standard collection class like Array. List –

3 ways to implement: • Derive from standard collection class like Array. List – Hide methods that aren’t typesafe – Properties that reference type Object using Shadows or new and replace with type-specific versions • Create a class that implements Ilist – Leaves internal storage up to you • Derive from Collection. Base

Collection. Base • Implements methods: Clear, Count • Type-specific up to you: – Add

Collection. Base • Implements methods: Clear, Count • Type-specific up to you: – Add – Remove – Insert – Item – indexer property • Protect property: List – Returns reference to Collection. Base’s IList implementation

Collection. Base Interfaces: • IEnumberable – allows For Each loop • IList – Standard

Collection. Base Interfaces: • IEnumberable – allows For Each loop • IList – Standard list methods • Add • Remove • Count – Can be bound to list-using UI • Datagrids • Listboxes • Comboboxes

Design-time support • Like dataset – available to Forms Designer • IComponent – Dragged

Design-time support • Like dataset – available to Forms Designer • IComponent – Dragged and dropped onto design surface – System. Windows. Froms. Control – System. Component. Model. Component • ISite interface – Returns property called Site • contains a reference to a component and the container it is in. • Used by designer to track object hierarchy • Disposed event – IDisposeable – Dispose method – free up resources – Clear List, then Raise the Disposed event

Data. Binding Support • IBinding. List interface • Add and Remove items • Sort

Data. Binding Support • IBinding. List interface • Add and Remove items • Sort based on selected column • Search for a value – R/O Boolean values to tell UI control whether user can • Allow. New • Allow. Edit • Alllow. Remove – Tell UI what features this collection supports • Supports. Change. Notification – List. Changed event • Supports. Searching • Supports. Sorting – For Sorting implementation • Is. Sorted

Editing Support • IEditable. Object interface – Allow. Edit • Row based editing •

Editing Support • IEditable. Object interface – Allow. Edit • Row based editing • Begin. Edit – user navigates to control • End. Edit – User navigates away from control – Changes should be saved • Cancel. Edit – User navigates away from control – Changes should be discarded • Need to track state of properties before and after editing has begun

Tracking State • Has Begin. Edit already been called? • Create a new Hashtable

Tracking State • Has Begin. Edit already been called? • Create a new Hashtable – store original values of properties • Helper class in Component. Model to create Property. Descriptor. Collection – List of Property. Descriptor objects • iterate through list and adds value of each property to Hashtable – use Property. Descriptor as index

Handle cancel from UI • User adds a Student in Data. Grid then cancels

Handle cancel from UI • User adds a Student in Data. Grid then cancels the edit – need to remove item from collection manually • m_Is. Add. New – bool tells it was added by user – End. Edit checks – True -> raised event Cancel. Add. New • Cancel. Edit replaces current property values with values from Hashtable • Sets Hashtable to Nothing – False -> save • Discard original property values • Set m_Editing flag to false

Student. Collection • IBinding. List. Add. New – cover in case user changes their

Student. Collection • IBinding. List. Add. New – cover in case user changes their mind – In constructor add event handler for Cancel. Edit – Cancel. Add. New unwires this event handler • IBinding. List. Changed – List. Changed. Event. Args • Type of change – inserted, deleted, modified, moved • Index of the item – Conllection. Base - override • On. Insert. Complete • On. Remove. Complete

Sorting Support • IComparer – Compare() • Comparison – Higher: 1 – Lower: -1

Sorting Support • IComparer – Compare() • Comparison – Higher: 1 – Lower: -1 – Equal: 0 • Student. Comparer – Compare. Properties – Sort order – direction modifier – Property. Descriptor - property to sort on

Sorting Support • IBinding. List. Apply. Sort – IBinding. List. Sorting. Direction – IBinding.

Sorting Support • IBinding. List. Apply. Sort – IBinding. List. Sorting. Direction – IBinding. List. Sorting. Property – Original order needs to be saved to undo • Create new Array. List based on original list – IBinding. List. Remove. Sort – IBinding. List. Is. Sorted • Collection. Base. List – IList implementation – wraps internal Array. List – Exposes Inner. List. Sort

Searching Support • Selected. Value, Selected. Index – Find that value – Make it

Searching Support • Selected. Value, Selected. Index – Find that value – Make it the current item – IBinding. List. Find • Property. Descriptor – what field – Data. Member • value to find – When it finds a match it returns index of item