Create a Comparable Object

Problem

You need to provide a mechanism that allows two custom objects to be compared.

Solution

Determine what data you want to use for the basis of your comparison, and implement the IComparable interface.

Discussion

The IComparable interface defines a single CompareTo method that accepts an object for comparison and returns an integer. The integer can take one of the following three values:

  • Zero, which means the objects are equal.

  • Less than zero (typically - 1), which means that the current object is less than the object that was supplied as a parameter.

  • Greater than zero (typically 1), which means that the current object is greater than the object that was supplied as a parameter.

You can evaluate the object contents on your ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.