Export (0) Print
Expand All

Enum.CompareTo Method

Compares this instance to a specified object and returns an indication of their relative values.

[Visual Basic]
Public Overridable Function CompareTo( _
   ByVal target As Object _
) As Integer
[C#]
public virtual int CompareTo(
 object target
);
[C++]
public: virtual int CompareTo(
 Object* target
);
[JScript]
public function CompareTo(
   target : Object
) : int;

Parameters

target
An object to compare, or a null reference (Nothing in Visual Basic).

Return Value

A signed number indicating the relative values of this instance and target.

Return Value Description
Less than zero The value of this instance is less than the value of target.
Zero The value of this instance is equal to the value of target.
Greater than zero The value of this instance is greater than the value of target.

-or-

target is a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
ArgumentException target and this instance are not the same type.
InvalidOperationException This instance is not type SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64.

Example

[Visual Basic, C#, C++] The following code sample illustrates the use of CompareTo in the context of Enum:

[Visual Basic] 
Imports System

Public Class CompareToTest
    
    Enum VehicleDoors
        Motorbike = 0
        Sportscar = 2
        Sedan = 4
        Hatchback = 5
    End Enum
    
    Public Shared Sub Main()
        Dim myVeh As VehicleDoors = VehicleDoors.Sportscar
        Dim yourVeh As VehicleDoors = VehicleDoors.Motorbike
        Dim otherVeh As VehicleDoors = VehicleDoors.Sedan
        
        Dim output as String

        If myVeh.CompareTo(yourVeh) > 0 Then output = "Yes" Else output = "No"
        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, yourVeh)
        Console.WriteLine("{0}{1}", output, Environment.NewLine)
        
        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, otherVeh)
        If myVeh.CompareTo(otherVeh) > 0 Then output = "Yes" Else output = "No"
        Console.WriteLine("{0}", output)
    End Sub 'Main
End Class 'CompareToTest

[C#] 
using System;

public class CompareToTest {
    enum VehicleDoors { Motorbike = 0, Sportscar = 2, Sedan = 4, Hatchback = 5 };

    public static void Main() {
        VehicleDoors myVeh = VehicleDoors.Sportscar;
        VehicleDoors yourVeh = VehicleDoors.Motorbike;
        VehicleDoors otherVeh = VehicleDoors.Sedan;

        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, yourVeh);
        Console.WriteLine( "{0}{1}", myVeh.CompareTo(yourVeh) > 0 ? "Yes" : "No", Environment.NewLine );

        Console.WriteLine("Does a {0} have more doors than a {1}?", myVeh, otherVeh);
        Console.WriteLine( "{0}", myVeh.CompareTo(otherVeh) > 0 ? "Yes" : "No" );
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

__value enum VehicleDoors { Motorbike=0, Sportscar=2, Sedan=4, Hatchback=5 };

int main() {
    VehicleDoors myVeh = VehicleDoors::Sportscar ;
    VehicleDoors yourVeh = VehicleDoors::Motorbike ;
    VehicleDoors otherVeh = VehicleDoors::Sedan ;
    
    Console::WriteLine("Does a {0} have more doors than a {1}?", __box(myVeh), __box(yourVeh));
    Int32 iRes = __box(myVeh)->CompareTo(__box(yourVeh));
    Console::WriteLine( "{0}{1}", iRes > 0 ? S"Yes" : S"No", Environment::NewLine );

    Console::WriteLine("Does a {0} have more doors than a {1}?", __box(myVeh), __box(otherVeh));
    iRes = __box(myVeh)->CompareTo(__box(otherVeh));
    Console::WriteLine( "{0}", iRes > 0 ? S"Yes" : S"No" );

}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Enum Class | Enum Members | System Namespace | Equals

Show:
© 2014 Microsoft