IComparable.CompareTo Method
Assembly: mscorlib (in mscorlib.dll)
'Declaration Function CompareTo ( _ obj As Object _ ) As Integer 'Usage Dim instance As IComparable Dim obj As Object Dim returnValue As Integer returnValue = instance.CompareTo(obj)
int CompareTo ( Object obj )
function CompareTo ( obj : Object ) : int
Parameters
- obj
An object to compare with this instance.
Return Value
A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value | Meaning |
---|---|
Less than zero | This instance is less than obj. |
Zero | This instance is equal to obj. |
Greater than zero | This instance is greater than obj. |
This method is only a definition and must be implemented by a specific class or value type to have effect. The meaning of the comparisons, "less than," "equal to," and "greater than," depends on the particular implementation.
By definition, any object compares greater than a null reference (Nothing in Visual Basic), and two null references compare equal to each other.
The parameter, obj, must be the same type as the class or value type that implements this interface; otherwise, an ArgumentException is thrown.
Notes to Implementers For objects A, B and C, the following must be true: A.CompareTo(A) is required to return zero. If A.CompareTo(B) returns zero, then B.CompareTo(A) is required to return zero. If A.CompareTo(B) returns zero and B.CompareTo(C) returns zero, then A.CompareTo(C) is required to return zero. If A.CompareTo(B) returns a value other than zero, then B.CompareTo(A) is required to return a value of the opposite sign. If A.CompareTo(B) returns a value x not equal to zero, and B.CompareTo(C) returns a value y of the same sign as x, then A.CompareTo(C) is required to return a value of the same sign as x and y. Notes to Callers Use the CompareTo method to determine the ordering of instances of a class.The following code sample illustrates the use of CompareTo to compare a Temperature object implementing IComparable with another object.
Public Class Temperature Implements IComparable Public Overloads Function CompareTo(ByVal obj As Object) As Integer _ Implements IComparable.CompareTo If TypeOf obj Is Temperature Then Dim temp As Temperature = CType(obj, Temperature) Return m_value.CompareTo(temp.m_value) End If Throw New ArgumentException("object is not a Temperature") End Function ' The value holder Protected m_value As Integer Public Property Value() As Integer Get Return m_value End Get Set(ByVal Value As Integer) m_value = Value End Set End Property Public Property Celsius() As Integer Get Return (m_value - 32) / 2 End Get Set(ByVal Value As Integer) m_value = Value * 2 + 32 End Set End Property End Class
public class Temperature implements IComparable { /// <summary> /// IComparable.CompareTo implementation. /// </summary> public int CompareTo(Object obj) { if (obj instanceof Temperature) { Temperature temp = (Temperature)obj; return ((Int32)mValue).CompareTo(temp.mValue); } throw new ArgumentException("object is not a Temperature"); } //CompareTo // The value holder protected int mValue; /** @property */ public int get_Value() { return mValue; }//get_Value /** @property */ public void set_Value(int value) { mValue = value; }//set_Value /** @property */ public int get_Celsius() { return (mValue - 32) / 2; }//get_Celsius /** @property */ public void set_Celsius(int value) { mValue = value * 2 + 32; }//set_Celsius } //Temperature
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.