Export (0) Print
Expand All

Enum.CompareTo Method

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Function CompareTo ( _
	target As Object _
) As Integer

Parameters

target
Type: System.Object
An object to compare, or Nothing.

Return Value

Type: System.Int32
A signed number that indicates the relative values of this instance and target.

Value

Meaning

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 Nothing.

Implements

IComparable.CompareTo(Object)

ExceptionCondition
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.

NullReferenceException

This instance is Nothing.

The following example illustrates the use of CompareTo in the context of Enum.


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
End Class
' The example displays the following output:
'       Does a Sportscar have more doors than a Motorbike?
'       Yes
'       
'       Does a Sportscar have more doors than a Sedan?
'       No


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft