Braintique.com header
Left Navigation Bar

Implementing IComparable

It’s time to get started with some classes that demonstrate implementing interfaces. For this demonstration, I’ll implement IComparable, which allows an ordered sort of objects of the same type that implement the interface. The example uses a base Dinosaur class, and specialized dinosaur classes—Allosaurus, TRex, etc.—derived from the base Dinosaur class. This is not particularly functional in the real world (after all, the dinosaurs are extinct).

But it should show you how to implement an interface.

Here’s the base Dinosaur class without any specifying any interfaces to implement:

public class Dinosaur
{
private string m_Name;
public int Length = 0;
public string Name {
get {
return m_Name;
}
set {
m_Name = value;
}
}
}

As you can see, the Dinosaur class is a pretty simple affair, consisting of a public property (Name) and a Length field.

Next, let’s add some specific dinosaur classes that derive from Dinosaur via inheritance:

class Lessemsaurus : Dinosaur {
public Lessemsaurus (string theName, int length){
this.Name = theName;
this.Length = length;
}
}

class Diplodocus : Dinosaur {
public Diplodocus (string theName, int length){
this.Name = theName;
}
}

class Allosaurus : Dinosaur {
public Allosaurus (string theName, int length){
this.Name = theName;
this.Length = length;
}
}
class TRex : Dinosaur {
public TRex (string theName, int length){
this.Name = theName;
this.Length = length;
}
}

You can now go back to the Dinosaur class and implement the IComparable interface using the same colon (:) operator used for inheritance:

public class Dinosaur : IComparable

By implementing the IComparable interface in the Dinosaur class, it will also, of course, be implemented in all derived classes, e.g., Allosaurus, Trex, etc.

Here’s the Dinosaur class as it stands:

public class Dinosaur : IComparable
{
private string m_Name;
public int Length = 0;
public string Name {
get {
return m_Name;
}
set {
m_Name = value;
}
}
}

As the code stands, IComparable is specified in the inheritance clause of the Dinosaur class, but there is no actual implementation in Dinosaur or its derived classes of the members specified in the IComparable “contract.” If you attempt to compile it, the project will not run, and you’ll get a syntax error pinpointing the problem.

To actually implement the IComparable interface, the class needs to include a CompareTo method. For example:

public int CompareTo (object obj) {
Dinosaur dino = (Dinosaur) obj;
return this.Name.CompareTo (dino.Name);
}

Depending on how you look at things, this CompareTo method can be considered a pretty trivial implementation of IComparable—or else cheating. All it does is cast the passed object to type Dinosaur, and then take advantage of the ordinary String object’s implementation to do a comparison based on the Name property of the Dinosaur class. In other words, it just sorts by the name of the class instance.

To see what implementing IComparable allows you to do, suppose you have an array of Dinosaur objects:

jPark = new Dinosaur [4];
jPark[0] = new Lessemsaurus("lessaemsaurus", 7);
jPark[1] = new Allosaurus("allosaurus", 12);
jPark[2] = new TRex("tRex", 14);
jPark[3] = new Diplodocus("doc", 9);

The Sort method of the System.Array object will then order the Dinosau objects (since they implement IComparable):

Array.Sort (jPark);

You can see this when you display the array in a ListBox (named lstDino):

foreach (Dinosaur dino in jPark){
lstDino.Items.Add(dino.Name);
}

It’s time to turn to creating your own class interfaces, covered in the continuation of this article.

Previous Table of Contents Next


Google
 
Web www.braintique.com
www.digitalfieldguide.com www.googleplexblog.com


Home | Barticles | Blogs | Books | Services | FAQ | Contact

© Braintique.com. All rights reserved.

Search Engine Optimization







RSS 2.0 Syndication feed

Syndication Viewer

Our Web host:
IX WebHosting



Food for Your Brain! Get a Barticle! Questions Answered Books for You What We Can Do For You Contact Us Brain Food Questions Answered Books for You What We Can Do For You Frequently Asked Questions About Us Google Research Photoshop Wi-Fi and Wireless Networking The Natural Way to Write