The release number of this article has been CHS307484
For Microsoft Visual Basic .NET versions of this article, see
306961.
For Microsoft Visual C .NET versions of this article, see
312514.
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.collection
This task content
summary
Implementing the ICollection interface in the custom class Enumerator object to implement the GetEnumerator method Using for Each iteration through custom set
SUMMARY This step-by-step guide describes how to implement custom collections in Visual C # .NET. The .NET Framework Baseline provides a formal definition of a collection:
System.collections.icollection interface.
Back to top
Implement the ICOLLECTION interface in the custom class
Icollection interface
The Ienumerable interface is inherited.
The iCollection interface defines one
COPYTO method and three read-only properties:
Issynchronized,
Syncroot and
COUNT.
ICollection
Ienumerable interface inheritance
GeTenumerator method. Custom set class should be implemented
ICollection interface.
To achieve
If you follow these steps:
Create a Windows application in Visual C # .NET. In "Solution Explorer", right-click the project name, point to add, and then click Add Class to add a class module named CustomCollection. Add the following code example to the beginning of the class module to import the System.Collection namespace: use system.collections; use the following code example alternate to any other code in the module:
Public Class Customcollection: ICollection
{
Private int [] INTARR = {1,5,9};
Private int CT;
Public CustomCollection ()
{
CT = 3;
}
} For the sake of simplicity, the CustomCollection class contains an array with three integers and a count variable. Implement a COPYTO method, which uses an integer array and an index as a parameter. This method copies the items in a collection to this array, with the incoming index as the start position. To implement this method, paste the following code to the public CustomCollection constructor: void iCollection.copyto (array myarr, int index)
{
Foreach (Int I in Intarr)
{
Myarr.SetValue (I, INDEX);
INDEX = INDEX 1;
}
} Implemented the getNumerator method, which is inherited from the Ienumerable interface. The GetEnumerator method returns an enumerator object that iterable through the collection. After paste the following sample code to the COPYTO method: IEnumerator ienumerable.GeteNumerator ()
{
Return New Enumerator (INTARR);
}
To achieve these three read-only properties, paste the following code to getenumerator method: /////// The issynchronized boolean property returns true
// Collection Is Designed to Be Thread Safe; OtherWise, IT Returns False.Bool iCollection.issynchronized
{
get
{
Return False;
}
}
// The syncroot printy returns an Object, Which is buy for synchronizing
// the collection.this returns the instance of the object or return
// syncroot of other collections if The Collection Contains Other Collectes.
//
Object iCollection.syncroot
{
get
{
Return this;
}
}
// the count read-only property returns the number
// of items in the collection.
Int iCollection.count
{
get
{
Return CT;
}
}
Back to top
Enumerator Objects in GetENUMERATOR Method This section describes how to create iterative pass
Customcollection
ENUMERATOR class.
Paste the following code sample After the END CLASS statement in the class module: Public Class Enumerator: ienumerator
{
Private int [] INTARR
Private int cursor;
} Declare that INTARR private integer array makes it the element of the CustomCollection class when calling the GetEnumerator method. The Cursor field member keeps the current location during the enumeration process. Add a constructor of the parameter as a parameter and set partial INTARR to this Intarr. Paste the following code samples to the statement of the member field: public enumerator (int [] INTARR)
{
THIS.ITARR = INTARR;
Cursor = -1;
Implement the RESET and MOVENEXT methods. To do this, please paste the following code to the constructor: void ienumerator.reset ()
{
Cursor = -1;
}
Bool ienumerator.movenext ()
{
Cursor CURSOR ; Return (! (cursor == intarr.length); } RESET sets the cursor to -1, and MoveNext moves the cursor to the next element. If successful, MoveNext returns true. Implement the Current read-only property that returns the item referred to in the cursor. If the cursor is -1, it generates an INVALIDOPERATIONException class. Paste the following code to the MOVENEXT method: Object IEnumerator.current { get { IF ((Cursor <0) || (CURSOR == INTARR.LENGTH))) Throw new invalidopertyleException (); Return INTARR [CURSOR]; } } Back to top Use for Each iteration through custom set On the Design tab in Form1.cs, drag a button to the form. Double-click the button, then add the following sample code to the click event of the button: CustomCollection mycol = new customCollection (); foreach (Object myobj in mycol) MessageBox.show (MyObj.toString ()); press F5 to run the app, and then click the button. Please note that a message box appears to display the items in the custom set. What is the principle of working? For ve call GetENUMERATOR method to create ENUMERATOR object, and call The MOVENEXT method sets the cursor to the first item. Then, access Current attributes to get items in MyObj. Repeat this step until MoveNext returns False. Back to top The information in this article applies to: Microsoft Visual C # .NET (2002) Recent Updated: 2002-7-29 (1.0) Keyword Kbhowto KbhowTomaster KB307484