You can create your own collection class by inheriting from a large number of .NET Framework collection classes and adds your own custom features. In this topic, you will use inheritance to create a simple strong type collection from CollectionBase inheritance.
.NET Framework provides a number of set types of collections in the System.Collections namespace. Some classes (such as stack, queue, and dictionary) are implemented to complete specific classes of specific tasks. Some classes (such as CollectionBase and DictionaryBase are already have certain basic functions, but most of the Mustinherit (Abstract) classes that are all accomplished to developers.
The CollectionBase class already has an implementation of the Clear method and count attribute. It maintains a protected property called List and uses the property for internal storage and organization. Other methods (such as add and remove) and Item properties need to be implemented.
In this exercise, you create a class called WidgetCollection using the CollectionBase class. It is a collection that only accepts widgets and is disclosed as a Widget type, rather than accepting objects and disclosing members as Object types. You then implement the widget to add a widget to a collection of widgets at the appropriate index, and you also implement the ITEM property to return a small part object at the appropriate index.
Create class
The first step is to create a Widget class to be placed in WidgetCollection.
Create a Widget class
'Visual BasicPublic Class Widget Public Name As Stringend Class Creates WidgetCollection Class' Visual BasicPublic Class WidgetCollection Inherits System.Collections.CollectionBasend Class implementation add and remove methods Implement Add and REMOVE
Now you will implement the Add method so that WidgetCollection only adds a Widget object.
'Restricts to widget types, items That Can Be Added To The Collection.Public Sub Add (Byval Awidget AS Widget)' Invokes Add Method of The List Object To Add A Widget. List.Add (AWIDGET) End Sub In this method, You will add the parameters of the Add method to the items in the List object to the Widget type. Although the List object can accept any type of object, the method is forbidden to add any objects other than the Widget type and act as a "package" of the List object.
Now you have a way to add a widget to a collection. You must now implement methods of removing these widgets. Create a Remove method that accepts an index as a parameter and calls the list.removeat method by creating a similar manner to create an Add method.
Realization Remove method 'Visual BasicPublic Sub Remove (ByVal index as Integer)' Check to see if there is a widget at the supplied index If index> Count -. 1 Or index <0 Then 'If no widget exists, a messagebox is shown and The Operation is 'Cancelled. System.Windows.Forms.MessageBox.show ("INDEX NOT VALID!") Else' Invokes The Removeat Method of the List Object. list.removeat (index) End IFEND SUB This method accepts integer values as an index parameter. If this value is valid, it will be passed to the REMOVEAT method of the List object, thereby removing the item located at the indicated index from the collection. To accomplish the basic collection function, you need to achieve the last part, that is, the ITEM property. The Item property allows you to get references to a certain object in a collection by reference to an index. In view of the Add method you already have to add members to a collection, Item will be the ReadOnly property, but don't have this in other contexts. Since the C # does not allow the properties with parameters, if C # is used, Item is required to be implemented as a method. Item property to achieve 'This line declares the Item property as ReadOnly, and' declares that it will return a Widget object.Public ReadOnly Property Item (ByVal index as Integer) As Widget Get 'The appropriate item is retrieved from the List object and' explicitly Cast to the Widget Type, Then Returned to the 'Caller. Return CType (Index), Widget) End Getend Property In a collection, syntax Collection.Item (0) and Collection (0) can often be interchangeable. If you want a collection to support this syntax, you should make the item attribute be a default property (in Visual Basic) or implement the indexer (in C #). For more information, see
Component's default properties
. Through verification, inheriting the class of CollectionBase can be sequentially, reverse sequentially, you can save, return to the class set of inherited CollectionBase in ViewState, and if you inherit ListArray, you cannot implement the definition sequence of data. Data loss when maintaining the status maintenance of the class set with ViewState, especially when there is a collection of nesting