It's really easy to implement one or more interfaces, and technically because it is not a class, multiple interfaces can be implemented at once. An interface is implemented as if it was a class:
class AClass : Class, ISomething, IInterface, IMoreInterface { ..members }
Interfaces provided by Microsoft There are hundreds of interfaces that shipped with the .NET Framework SDK. Those interfaces are essential within the framework as they hold's the framework's structure and ensure compatibility between objects. They also allow 3rd party additions of objects to work flawlessly with Microsoft's prewritten library of objects.
IEnumerator and IEnumerable are the two most commonly used interfaces and we will discuss them in detail now.
These Interfaces reside in the System.Collections namespace.
Firstly let's have a look at each interface we are dealing with here. IEnumerator is an interface that has following members:Property: CurrentMethod: MoveNextMethod: ResetMSDN describes the IEnumerator class as "Supports a simple iteration over a collection.", as well as:
"IEnumerator is the base interface for all enumerators. Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.
Initially, the enumerator is positioned before the first element in the collection. Reset also brings the enumerator back to this position. At this position, calling Current throws an exception. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current."
IEnumerable is an interface that has 1 method called GetEnumerator. The MSDN documentation describes the GetEnumerator method as "Exposes the enumerator, which supports a simple iteration over a collection."... It then goes on to say "IEnumerable must be implemented to support the ForEach semantics of Microsoft Visual Basic. COM classes that allow enumerators also implement this interface".
As you can see, MSDN plays an important role in .NET development by providing the developers with correct information. In this case, MSDN provides what each method or property in the interface should do when implemented by a class.
So now that we know about the interfaces we are dealing with, let's start making some collection objects. What we are going to make today is a collection called Customers, which will contain Customer objects. I chose this because this collection seemed to be used a lot.
First, we need to make a Customer class:
class Customer { public Customer(int customerID) { this.customerID=customerID; } private int customerID; public int CustomerID { get { return customerID; } set { customerID =value; } } private string name; public string Name { get { return name; } set { name =value; } } }
This is a fairly simple class, and contains just two properties. One returns the customers name, and the other returns the customers id. Now, we want to make a Customer collection class that is enumerable but at the same time has methods to sort customers by different fields.
On top of the methods implemented by the interfaces, it will also have a method called SortByName(), which will sort the items in the collection by the field 'name' and reset the collection.
There will be another method called SortByID(), which will sort the items in the collection by the field 'customerID' and then reset the collection.
Here's the code for this collection class:
class Customers : System.Collections.IEnumerable, System.Collections.IEnumerator { System.Collections.ArrayList customerIDs; System.Collections.IEnumerator ienum;
public void SortByName() { // Get all customerIDs from the database sorted by name
this.Reset(); } public void SortByID() { // Get all customerIDs from the database sorted by customerID
this.Reset(); } public Customers() { // Get all customerIDs from the database and is put in to an ArrayList, // customerIDs
// Gets ienumerator interface for customerIDs arraylist ienum = customerIDs.GetEnumerator(); } public System.Collections.IEnumerator GetEnumerator() { // Polymorph this object into an IEnumerator interface return (System.Collections.IEnumerator)this; } public void Reset() { ienum.Reset(); } public bool MoveNext() { return ienum.MoveNext(); } public object Current { get { return new Customer(Convert.ToInt32(ienum.Current)); } } }
Basically, the MoveNext and Reset methods are the MoveNext and Reset methods of the ArrayList that we've created which behind the scenes polymorphed into an IEnumerator interface and returned when GetEnumerator is called.
The Current property just returns a new Customer object, instantiated when it is called with the current object of the customerIDs ArrayList.
No comments:
Post a Comment